﻿/*
*VMukti -- An open source video conferencing platform.
*
* Copyright (C) 2007 - 2008, VMukti Solutions Pvt. Ltd.
*
* Hardik Sanghvi <hardik@vmukti.com>
*
* See http://www.vmukti.com for more information about
* the VMukti project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using VMukti.Business;
using VMukti.Business.CommonDataContracts;
using VMukti.CtlResizer.Presentation;
using VMukti.Presentation.Controls;
using VMukti.Presentation.MainPod;
using VMuktiAPI;

namespace VMukti.Presentation.Page_Tab
{

    public partial class CtlPageTab:IDisposable
    {
        public ClsPageTabControl ptc = new ClsPageTabControl();

        int intIslost;
        double i = 0.0;
        int IsResizing = -1;

        public static double newWidth = 0.0;
        public static double newHeight = 0.0;

        public static double cnvmainHeight;
        public static double cnvmainWidth;
        public static double svbuddylistwidth;
        public static double svloginWidth;

        bool blnModuleflag = false;

        public bool blnBuddyType = true;
        //public List<string> lstUsersDropped = new List<string>();

        CtlPod c = null;
        CtlPod dynCtlPod = null;

        public delegate void DelChannelSetMsg(string from, string to, string msg, VMukti.Business.CommonDataContracts.clsModuleInfo cmi);
        public event DelChannelSetMsg EntChannelSetMsg;

        public delegate void DelChannelSetPageMsg(string from, string to, string msg, List<VMukti.Business.CommonDataContracts.clsModuleInfo> cmi);
        public event DelChannelSetPageMsg EntChannelSetPageMsg;

        public delegate void DelChannelRemoveBuddy(string from, List<string> to, string msg, VMukti.Business.CommonDataContracts.clsModuleInfo cmi);
        public event DelChannelRemoveBuddy EntChannelRemoveDraggedBuddy;


        bool blnRename = false;

        #region KeyStroke

        [DllImport("user32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

        [DllImport("User32.dll")]
        public static extern int FindWindow(string lpClassName, string lpWindowName);

        [DllImport("User32.dll")]
        public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);


        const byte KEYEVENTF_KEYUP = 2;
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_CLOSE = 0xF060;

        const byte VK_Tab = 9;

        #endregion

        public CtlPageTab()
        {
            try
            {
                intIslost = 1;
                ptc.SizeChanged += new SizeChangedEventHandler(ptc_SizeChanged);
                
            }
            catch (Exception exp)
            {
                exp.Data.Add("My key", "CtlPageTab:CtlPageTab.cs" + exp.Message);
                ClsException.LogError(exp);
            }
        }

        public void LoadDefaultPage(double cnvMainHeight, double cnvMainWidth, double svLoginWidth, double svBuddyListWidth)
        {

            cnvmainHeight = cnvMainHeight;
            cnvmainWidth = cnvMainWidth;
            svbuddylistwidth = svBuddyListWidth;
            svloginWidth = svLoginWidth;

            ptc.HorizontalAlignment = HorizontalAlignment.Stretch;
            ptc.VerticalAlignment = VerticalAlignment.Stretch;

            #region LoadingDefaultPage and related Tabs

            try
            {
                ClsTabsTabItem tti = new ClsTabsTabItem();
                tti.Header = "Default Page";
                tti.Foreground = Brushes.Black;
                //tti.Background = (Brush)App.Current.Resources["TabItemDisabledBackground"];
                tti.Background = Brushes.Transparent;
                tti.Tag = "0";
                tti.TabTag = "0";
                tti.TabID = "0";
                tti.AllowDrop = true;
                tti.Drop += new DragEventHandler(tti_Drop);
                //tti.PreviewDrop += new DragEventHandler(tti_PreviewDrop);

                #region PageDeclarations

                ClsPageTabControl ptcTabs = new ClsPageTabControl();
                //ptcTabs.Height = cnvMainHeight;
                //ptcTabs.Width = cnvMainWidth - svLoginWidth - svBuddyListWidth - 2.0;
                ptcTabs.Tag = "0";
                ptcTabs.PageID = 0;
                ptcTabs.PageTag = "0";
                ptcTabs.PageTitle = "Default Page";
                ptcTabs.Background = Brushes.Transparent;
                ptcTabs.Foreground = Brushes.Black;
                ptcTabs.BorderBrush = Brushes.Transparent;
                ptcTabs.SetValue(TabItem.MarginProperty, new Thickness(0, 2.0, 0, 0));
                //ptcTabs.AllowDrop = true;
                //ptcTabs.Drop += new DragEventHandler(ptcTabs_Drop);
                //ptcTabs.PreviewDrop += new DragEventHandler(ptcTabs_PreviewDrop);


                #endregion

                #region ScrollViewer+Canvas as container for Pods

                Canvas cnvNew = new Canvas();
                cnvNew.Background = Brushes.Transparent;
                //cnvNew.Height = sv.Height + 30;
                //cnvNew.Width = sv.Width + 100;   
                cnvNew.HorizontalAlignment = HorizontalAlignment.Stretch;
                cnvNew.VerticalAlignment = VerticalAlignment.Stretch;
                cnvNew.AllowDrop = true;
                cnvNew.MouseMove += new System.Windows.Input.MouseEventHandler(cnvNew_MouseMove);
                cnvNew.Drop += new DragEventHandler(cnvNew_Drop);
                cnvNew.SizeChanged += new SizeChangedEventHandler(cnvNew_SizeChanged);

                #endregion

                #region TabItem

                ClsTabsTabItem ttinew = new ClsTabsTabItem();
                ttinew.Header = "Tab1";
                ttinew.Tag = "0,0";
                ttinew.TabID = "0,0";
                ttinew.TabTitle = "Tab1";
                ttinew.TabTag = "0,0";

                ttinew.Background = Brushes.Transparent;
                ttinew.Foreground = Brushes.Black;
                ttinew.Height = 20;
                ttinew.Width = 150; //120
                ttinew.Content = cnvNew;
                ttinew.AllowDrop = true;
                ttinew.Drop += new DragEventHandler(ttinew_Drop);
                //ttinew.PreviewDrop += new DragEventHandler(ttinew_PreviewDrop);                

                ClsModuleCollection objCMC = null;
                objCMC = ClsModuleCollection.GetNonAuthenticatedMod();

                double setTop = 5.0;
                double setLeft = 5.0;
                int counter = 0;

                for (int PCnt = 0; PCnt < objCMC.Count; PCnt++)
                {
                    ClsPermissionCollection objCPC = ClsPermissionCollection.Get_PermissionRefModule(objCMC[PCnt].ModuleId, VMuktiAPI.VMuktiInfo.CurrentPeer.RoleID);

                    int[] arrPermissionValue = new int[objCPC.Count];

                    for (int percount = 0; percount < objCPC.Count; percount++)
                    {
                        arrPermissionValue[percount] = objCPC[percount].PermissionValue;
                    }
                    c = new CtlPod(objCMC[PCnt].ModuleId, objCMC[PCnt].ModuleTitle, "False", null, arrPermissionValue, false);
                    c.Height = 200;
                    c.Width = 200;
                    c.DBHeight = c.Height;
                    c.DBWidth = c.Width;
                    c.ModID = objCMC[PCnt].ModuleId;
                    c.Text = objCMC[PCnt].ModuleTitle;
                    c.PageId = "0";
                    c.SetValue(Canvas.TopProperty, setTop);
                    c.SetValue(Canvas.LeftProperty, setLeft);
                    c.PID = 0;
                    c.TabId = "0,0";
                    c.EntSavePage += new CtlPod.DelSavePage(c_EntSavePage);
                    c.EntMainPodMouseDown += new CtlPod.DelMainPodMouseDown(c_EntMainPodMouseDown);
                    c.PreviewMouseUp += new MouseButtonEventHandler(c_PreviewMouseUp);
                    c.SizeChanged += new SizeChangedEventHandler(c_SizeChanged);
                    Resizer r = new Resizer();
                    r.SetValue(Canvas.LeftProperty, setLeft);
                    r.SetValue(Canvas.TopProperty, setTop);
                    c.DBTop = setTop;
                    c.DBLeft = setLeft;
                    setLeft += c.Width + 65.0;
                    counter++;
                    if (counter == 2)
                    {
                        setLeft = 5.0;
                        setTop += c.Height + 30.0;
                        counter = 0;
                    }
                    r.Content = c;
                    r.Background = Brushes.Transparent;
                    r.SizeChanged += new SizeChangedEventHandler(r_SizeChanged);
                    r.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(r_PreviewMouseLeftButtonDown);
                    r.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(r_PreviewMouseLeftButtonUp);
                    cnvNew.Children.Add(r);
                }
                ptcTabs.Items.Add(ttinew);

                #endregion

                #region Plus TabItem

                ClsTabsTabItem ttiPlus = new ClsTabsTabItem();
                ttiPlus.Header = "+";
                ttiPlus.Tag = "0,-1";
                ttiPlus.TabID = "0,-1";
                ttiPlus.TabTag = "0,-1";
                ttiPlus.TabTitle = "+";

                ttiPlus.Background = Brushes.Transparent;
                ttiPlus.Foreground = Brushes.Black;
                ttiPlus.Height = 20;
                ttiPlus.Width = 150;            // chang =100;

                ttiPlus.Loaded += new RoutedEventHandler(ttiPlus_Loaded);
                ttiPlus.PreviewMouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(ttiPlus_PreviewMouseLeftButtonDown);

                ptcTabs.Items.Add(ttiPlus);

                #endregion

                tti.Content = ptcTabs;
                ptc.Items.Add(tti);
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "LoadDefaultPage()--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
            }

            #endregion
        }

        //tab
        void ttinew_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(CtlExpanderItem)))
            {
                string[] strSplitted = e.Source.ToString().Split(':');
                if (strSplitted != null)
                {
                    if (strSplitted[0] == "VMukti.Presentation.Page_Tab.ClsTabsTabItem Header")
                    {
                        string[] strId = ((ClsTabsTabItem)e.Source).TabID.Split(',');
                        if (strId != null)
                        {
                            if (strId.Length > 1)
                            {
                                //MessageBox.Show("Dragged On Tab");
                                #region Check whether it is module or buddy dropped

                                bool flag = false;
                                string[] strTag = ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Tag.ToString().Split(',');
                                List<string> lstTag = new List<string>();
                                for (int i = 0; i < strTag.Length; i++)
                                {
                                    //lstTag.Add(strTag[i]);
                                    if (strTag[i] == "ModuleType")
                                    {
                                        blnBuddyType = false;
                                        break;
                                    }
                                }

                                #endregion

                                if (blnBuddyType)
                                {
                                    if (((ClsTabsTabItem)e.Source).TabTitle != "+")
                                    {
                                        #region Save Page
                                        if (strId[0] == "0" || strId[0] == "-1")
                                        {

                                            MessageBoxResult r;
                                            r = MessageBox.Show("Do you want to save the page", "Save Page", MessageBoxButton.YesNoCancel, MessageBoxImage.Question, MessageBoxResult.Yes);
                                            if (r == MessageBoxResult.Yes)
                                            {
                                                int pageid = new ClsPageLogic().Add_Page(-1,
                                                    ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).PageTitle,
                                                    "",
                                                    true,
                                                    VMuktiAPI.VMuktiInfo.CurrentPeer.ID);

                                                ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).PageID = pageid;
                                                ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Tag = pageid;
                                                ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).PageTag = pageid.ToString();

                                                ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Parent).TabID = pageid.ToString();
                                                ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Parent).Tag = pageid;
                                                ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Parent).TabTag = pageid.ToString();



                                                for (int tabcount = 0; tabcount < ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Items.Count; tabcount++)
                                                {
                                                    if (((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Items[tabcount]).TabTitle != "+")
                                                    {
                                                        int tabid = new ClsTabLogic().Add_Tab(-1,
                                                            pageid, 1,
                                                            ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Items[tabcount]).TabTitle,
                                                            "",
                                                            VMuktiAPI.VMuktiInfo.CurrentPeer.ID);

                                                        ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Items[tabcount]).Tag = pageid.ToString() + "," + tabid;
                                                        ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Items[tabcount]).TabTag = pageid.ToString() + "," + tabid;
                                                        ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Items[tabcount]).TabID = pageid.ToString() + "," + tabid;


                                                        for (int podcount = 0; podcount < ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Items[tabcount]).Content).Children.Count; podcount++)
                                                        {
                                                            int podid = ClsPod.AddPod(-1, tabid.ToString(),
                                                                 ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Items[tabcount]).Content).Children[podcount]).Content).Text,
                                                                 ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Items[tabcount]).Content).Children[podcount]).Content).Height,
                                                                 ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Items[tabcount]).Content).Children[podcount]).Content).Width,
                                                                 double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Items[tabcount]).Content).Children[podcount]).GetValue(Canvas.LeftProperty).ToString()),
                                                                 double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Items[tabcount]).Content).Children[podcount]).GetValue(Canvas.TopProperty).ToString()),
                                                                 "",
                                                                 ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Items[tabcount]).Content).Children[podcount]).Content).ModID,
                                                                 VMuktiAPI.VMuktiInfo.CurrentPeer.ID);

                                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Items[tabcount]).Content).Children[podcount]).Content).PID = podid;
                                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Items[tabcount]).Content).Children[podcount]).Content).PageId = pageid.ToString();
                                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Items[tabcount]).Content).Children[podcount]).Content).TabId = tabid.ToString();

                                                        }
                                                    }
                                                }

                                                #region Create Message

                                                List<clsModuleInfo> lstModuleInfo = new List<clsModuleInfo>();


                                                for (int pCnt = 0; pCnt < ((Canvas)((ClsTabsTabItem)e.Source).Content).Children.Count; pCnt++)
                                                {
                                                    clsModuleInfo cmi = new clsModuleInfo();



                                                    cmi.intModuleId = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).ModID;
                                                    cmi.strModuleName = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).Text;
                                                    cmi.ModPermissions = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).ModPer;


                                                    cmi.strUri = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).WCFUri;
                                                    cmi.strPodid = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).PID.ToString();


                                                    cmi.strPageid = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).PageId;
                                                    cmi.strTabid = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).TabId;

                                                    cmi.strDropType = "Tab Type";

                                                    lstModuleInfo.Add(cmi);

                                                }

                                                #endregion

                                                #region Send Message To Peer/s

                                                for (int i = 0; i < ((ClsTabsTabItem)e.Source).lstBuddyDragged.Count; i++)
                                                {
                                                    if (((ClsTabsTabItem)e.Source).lstBuddyDragged[i] == ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption)
                                                    {
                                                        flag = true;
                                                        break;
                                                    }
                                                }


                                                if (!flag)
                                                {
                                                    string droppedBName = ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption;
                                                    
                                                    //Add dropped username in list of tab
                                                    ((ClsTabsTabItem)e.Source).lstBuddyDragged.Add(droppedBName);

                                                    //Add dropped username in combobox of tab
                                                    ComboItem cbiTabBuddy = new ComboItem();
                                                    cbiTabBuddy.setName(droppedBName);
                                                    ((ClsTabsTabItem)e.Source).cmbTabBuddy.Items.Add(cbiTabBuddy);


                                                    //Add dropped username in related POD's combobox and list 
                                                    for (int pCnt = 0; pCnt < ((Canvas)((ClsTabsTabItem)e.Source).Content).Children.Count; pCnt++)
                                                    {
                                                        //Add dropped username in POD's combobox
                                                        ComboItem cbiPodBuddy = new ComboItem();
                                                        cbiPodBuddy.setName(droppedBName);
                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).cmbPodBuddy.Items.Add(cbiPodBuddy);

                                                        //Add dropped username in POD's list
                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).lstUsersDropped.Add(droppedBName);
                                                    }
                                                    ////Add dropped username in Page's combobox
                                                    //ComboItem cbiPageBuddy = new ComboItem();
                                                    //cbiPageBuddy.setName(droppedBName);
                                                    //((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Parent).cmbTabBuddy.Items.Add(cbiPageBuddy);

                                                    ////Add dropped username in Page's list
                                                    //((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Parent).lstBuddyDragged.Add(droppedBName);

                                                    for (int lstCnt = 0; lstCnt < lstModuleInfo.Count; lstCnt++)
                                                    {
                                                        lstModuleInfo[lstCnt].lstUsersDropped = ((ClsTabsTabItem)e.Source).lstBuddyDragged;
                                                    }
                                                    if (EntChannelSetPageMsg != null)
                                                    {
                                                        EntChannelSetPageMsg(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, droppedBName, "OPEN_PAGE", lstModuleInfo);
                                                    }
                                                }

                                                #endregion
                                            }

                                            else if (r == MessageBoxResult.No)
                                            {
                                                MessageBox.Show("Save the page to collaborate");
                                                return;
                                            }
                                            else
                                            {
                                                return;
                                            }
                                        }
                                        #endregion

                                        #region If page is saved, check whether tabs are saved
                                        else if (strId[0] != "0" || strId[0] != "-1")
                                        {
                                            if (strId[1] == "0" || strId[1] == "-1")
                                            {
                                                MessageBoxResult r;
                                                r = MessageBox.Show("Do you want to save the tab to collaborate", "Save Tab", MessageBoxButton.YesNoCancel, MessageBoxImage.Question, MessageBoxResult.Yes);
                                                if (r == MessageBoxResult.Yes)
                                                {
                                                    int tabid = new ClsTabLogic().Add_Tab(-1,
                                                        int.Parse(strId[0]), 1,
                                                    ((ClsTabsTabItem)e.Source).TabTitle,
                                                    "",
                                                    VMuktiAPI.VMuktiInfo.CurrentPeer.ID);

                                                    ((ClsTabsTabItem)e.Source).TabTag = strId[0] + "," + tabid.ToString();
                                                    ((ClsTabsTabItem)e.Source).Tag = strId[0] + "," + tabid.ToString();
                                                    ((ClsTabsTabItem)e.Source).TabID = strId[0] + "," + tabid.ToString();

                                                    for (int podcount = 0; podcount < ((Canvas)((ClsTabsTabItem)e.Source).Content).Children.Count; podcount++)
                                                    {
                                                        int podid = ClsPod.AddPod(-1,
                                                            tabid.ToString(),
                                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).Text,
                                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).Height,
                                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).Width,
                                                            double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).GetValue(Canvas.LeftProperty).ToString()),
                                                            double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).GetValue(Canvas.TopProperty).ToString()),
                                                            "",
                                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).ModID,
                                                            VMuktiAPI.VMuktiInfo.CurrentPeer.ID);

                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).PID = podid;
                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).PageId = strId[0];
                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).TabId = strId[0] + "," + tabid;


                                                    }
                                                }
                                                else if (r == MessageBoxResult.No)
                                                {
                                                    MessageBox.Show("Save the Tab to collaborate");
                                                    return;
                                                }
                                                else
                                                {
                                                    return;
                                                }

                                                #region Create Message

                                                List<clsModuleInfo> lstModuleInfo = new List<clsModuleInfo>();


                                                for (int pCnt = 0; pCnt < ((Canvas)((ClsTabsTabItem)e.Source).Content).Children.Count; pCnt++)
                                                {
                                                    clsModuleInfo cmi = new clsModuleInfo();



                                                    cmi.intModuleId = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).ModID;
                                                    cmi.strModuleName = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).Text;
                                                    cmi.ModPermissions = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).ModPer;


                                                    cmi.strUri = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).WCFUri;
                                                    cmi.strPodid = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).PID.ToString();


                                                    cmi.strPageid = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).PageId;
                                                    cmi.strTabid = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).TabId;

                                                    cmi.strDropType = "Tab Type";

                                                    lstModuleInfo.Add(cmi);

                                                }

                                                #endregion

                                                #region Send Message To Peer/s

                                                for (int i = 0; i < ((ClsTabsTabItem)e.Source).lstBuddyDragged.Count; i++)
                                                {
                                                    if (((ClsTabsTabItem)e.Source).lstBuddyDragged[i] == ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption)
                                                    {
                                                        flag = true;
                                                        break;
                                                    }
                                                }


                                                if (!flag)
                                                {
                                                    string droppedBName = ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption;

                                                    //Add dropped username in list of tab
                                                    ((ClsTabsTabItem)e.Source).lstBuddyDragged.Add(droppedBName);

                                                    //Add dropped username in combobox of tab
                                                    ComboItem cbiTabBuddy = new ComboItem();
                                                    cbiTabBuddy.setName(droppedBName);
                                                    ((ClsTabsTabItem)e.Source).cmbTabBuddy.Items.Add(cbiTabBuddy);


                                                    //Add dropped username in related POD's combobox and list 
                                                    for (int pCnt = 0; pCnt < ((Canvas)((ClsTabsTabItem)e.Source).Content).Children.Count; pCnt++)
                                                    {
                                                        //Add dropped username in POD's combobox
                                                        ComboItem cbiPodBuddy = new ComboItem();
                                                        cbiPodBuddy.setName(droppedBName);
                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).cmbPodBuddy.Items.Add(cbiPodBuddy);

                                                        //Add dropped username in POD's list
                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).lstUsersDropped.Add(droppedBName);
                                                    }
                                                    ////Add dropped username in Page's combobox
                                                    //ComboItem cbiPageBuddy = new ComboItem();
                                                    //cbiPageBuddy.setName(droppedBName);
                                                    //((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Parent).cmbTabBuddy.Items.Add(cbiPageBuddy);

                                                    ////Add dropped username in Page's list
                                                    //((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Parent).lstBuddyDragged.Add(droppedBName);

                                                    for (int lstCnt = 0; lstCnt < lstModuleInfo.Count; lstCnt++)
                                                    {
                                                        lstModuleInfo[lstCnt].lstUsersDropped = ((ClsTabsTabItem)e.Source).lstBuddyDragged;
                                                    }
                                                    if (EntChannelSetPageMsg != null)
                                                    {
                                                        EntChannelSetPageMsg(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption, "OPEN_PAGE", lstModuleInfo);
                                                    }
                                                }

                                                #endregion

                                            }
                                            else
                                            {
                                                for (int podcount = 0; podcount < ((Canvas)((ClsTabsTabItem)e.Source).Content).Children.Count; podcount++)
                                                {
                                                    if (((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).PID.ToString() == "0" || ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).PID.ToString() == "-1")
                                                    {
                                                        int podid = ClsPod.AddPod(-1,
                                                            ((ClsTabsTabItem)e.Source).TabID.Split(',')[1],
                                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).Text,
                                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).Height,
                                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).Width,
                                                            double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).GetValue(Canvas.LeftProperty).ToString()),
                                                            double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).GetValue(Canvas.TopProperty).ToString()),
                                                            "",
                                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).ModID,
                                                            VMuktiAPI.VMuktiInfo.CurrentPeer.ID);

                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).PID = podid;
                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).PageId = strId[0];
                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).TabId = strId[0] + "," + ((ClsTabsTabItem)e.Source).TabID.Split(',')[1];

                                                    }
                                                    else
                                                    {
                                                        if (((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).ResizePod)
                                                        {
                                                            int podid = ClsPod.AddPod(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).PID,
                                                           ((ClsTabsTabItem)e.Source).TabID.Split(',')[1],
                                                           ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).Text,
                                                           ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).Height,
                                                           ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).Width,
                                                           double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).GetValue(Canvas.LeftProperty).ToString()),
                                                           double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).GetValue(Canvas.TopProperty).ToString()),
                                                           "",
                                                           ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).ModID,
                                                           VMuktiAPI.VMuktiInfo.CurrentPeer.ID);
                                                        }
                                                    }
                                                }

                                                #region Create Message

                                                List<clsModuleInfo> lstModuleInfo = new List<clsModuleInfo>();


                                                for (int pCnt = 0; pCnt < ((Canvas)((ClsTabsTabItem)e.Source).Content).Children.Count; pCnt++)
                                                {
                                                    clsModuleInfo cmi = new clsModuleInfo();



                                                    cmi.intModuleId = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).ModID;
                                                    cmi.strModuleName = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).Text;
                                                    cmi.ModPermissions = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).ModPer;


                                                    cmi.strUri = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).WCFUri;
                                                    cmi.strPodid = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).PID.ToString();


                                                    cmi.strPageid = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).PageId;
                                                    cmi.strTabid = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).TabId;

                                                    cmi.strDropType = "Tab Type";

                                                    lstModuleInfo.Add(cmi);

                                                }

                                                #endregion

                                                #region Send Message To Peer/s

                                                for (int i = 0; i < ((ClsTabsTabItem)e.Source).lstBuddyDragged.Count; i++)
                                                {
                                                    if (((ClsTabsTabItem)e.Source).lstBuddyDragged[i] == ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption)
                                                    {
                                                        flag = true;
                                                        break;
                                                    }
                                                }


                                                if (!flag)
                                                {
                                                    string droppedBName = ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption;

                                                    //Add dropped username in list of tab
                                                    ((ClsTabsTabItem)e.Source).lstBuddyDragged.Add(droppedBName);

                                                    //Add dropped username in combobox of tab
                                                    ComboItem cbiTabBuddy = new ComboItem();
                                                    cbiTabBuddy.setName(droppedBName);
                                                    ((ClsTabsTabItem)e.Source).cmbTabBuddy.Items.Add(cbiTabBuddy);


                                                    //Add dropped username in related POD's combobox and list 
                                                    for (int pCnt = 0; pCnt < ((Canvas)((ClsTabsTabItem)e.Source).Content).Children.Count; pCnt++)
                                                    {
                                                        //Add dropped username in POD's combobox
                                                        ComboItem cbiPodBuddy = new ComboItem();
                                                        cbiPodBuddy.setName(droppedBName);
                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).cmbPodBuddy.Items.Add(cbiPodBuddy);

                                                        //Add dropped username in POD's list
                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[pCnt]).Content).lstUsersDropped.Add(droppedBName);
                                                    }
                                                    ////Add dropped username in Page's combobox
                                                    //ComboItem cbiPageBuddy = new ComboItem();
                                                    //cbiPageBuddy.setName(droppedBName);
                                                    //((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Parent).cmbTabBuddy.Items.Add(cbiPageBuddy);

                                                    ////Add dropped username in Page's list
                                                    //((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Parent).lstBuddyDragged.Add(droppedBName);

                                                    for (int lstCnt = 0; lstCnt < lstModuleInfo.Count; lstCnt++)
                                                    {
                                                        lstModuleInfo[lstCnt].lstUsersDropped = ((ClsTabsTabItem)e.Source).lstBuddyDragged;
                                                    }
                                                    if (EntChannelSetPageMsg != null)
                                                    {
                                                        EntChannelSetPageMsg(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption, "OPEN_PAGE", lstModuleInfo);
                                                    }
                                                }

                                                #endregion

                                            }
                                        }
                                        #endregion

                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        
        //page
        void tti_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(CtlExpanderItem)))
            {
                string[] strSplitted = e.Source.ToString().Split(':');
                if (strSplitted != null)
                {
                    if (strSplitted[0] == "VMukti.Presentation.Page_Tab.ClsTabsTabItem Header")
                    {
                        string[] strId = ((ClsTabsTabItem)e.Source).TabID.Split(',');
                        if (strId != null)
                        {
                            if (strId.Length == 1)
                            {
                                #region Check whether it is module or buddy dropped

                                bool flag = false;
                                string[] strTag = ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Tag.ToString().Split(',');
                                List<string> lstTag = new List<string>();
                                for (int i = 0; i < strTag.Length; i++)
                                {
                                    //lstTag.Add(strTag[i]);
                                    if (strTag[i] == "ModuleType")
                                    {
                                        blnBuddyType = false;
                                        break;
                                    }
                                }

                                #endregion

                                if (blnBuddyType)
                                {
                                    #region Save Page

                                    if (strId[0] == "0" || strId[0] == "-1")
                                    {
                                        MessageBoxResult r;
                                        r = MessageBox.Show("Do You want to save the page for collaboration? ", "Save Page", MessageBoxButton.YesNoCancel, MessageBoxImage.Question, MessageBoxResult.Yes);
                                        if (r == MessageBoxResult.Yes)
                                        {
                                            ClsPageLogic cpl = new ClsPageLogic();
                                            if (!(VMuktiAPI.VMuktiInfo.CurrentPeer.ID <= 0))
                                            {
                                                int intPageId = cpl.Add_Page(-1, ((ClsTabsTabItem)e.Source).Header.ToString(), "", true, VMuktiAPI.VMuktiInfo.CurrentPeer.ID);

                                                ((ClsTabsTabItem)e.Source).Tag = intPageId;
                                                ((ClsTabsTabItem)e.Source).TabID = intPageId.ToString();
                                                ((ClsTabsTabItem)e.Source).TabTag = intPageId.ToString();
                                                ((ClsPageTabControl)(((ClsTabsTabItem)e.Source).Content)).Tag = intPageId;
                                                ((ClsPageTabControl)(((ClsTabsTabItem)e.Source).Content)).PageID = intPageId;
                                                ((ClsPageTabControl)(((ClsTabsTabItem)e.Source).Content)).PageTag = intPageId.ToString();


                                                cpl.Page_Allocated(-1, intPageId, VMuktiAPI.VMuktiInfo.CurrentPeer.ID);

                                                for (int j = 0; j < ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items.Count; j++)
                                                {
                                                    if (((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).TabTitle != "+")
                                                    {
                                                        int intTabId = new ClsTabLogic().Add_Tab(-1, intPageId, 1, ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).TabTitle, "", VMuktiAPI.VMuktiInfo.CurrentPeer.ID);
                                                        ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Tag = intPageId + "," + intTabId;
                                                        ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).TabTag = intPageId + "," + intTabId;
                                                        ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).TabID = intPageId + "," + intTabId;


                                                        //save the related pods 



                                                        for (int k = 0; k < ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children.Count; k++)

                                                        //for (int k = 0; k < ((Canvas)((ScrollViewer)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Content).Content).Children.Count; k++)
                                                        {

                                                            int intPodId = ClsPod.AddPod(-1, intTabId.ToString(),
                                                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children[k]).Content).Text,
                                                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children[k]).Content).Height,
                                                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children[k]).Content).Width,
                                                                //double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children[k]).Content).GetValue(Canvas.LeftProperty).ToString()),
                                                                //double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children[k]).Content).GetValue(Canvas.TopProperty).ToString()),
                                                                double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children[k]).GetValue(Canvas.LeftProperty).ToString()),
                                                                double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children[k]).GetValue(Canvas.TopProperty).ToString()),
                                                                "",
                                                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children[k]).Content).ModID,
                                                                VMuktiAPI.VMuktiInfo.CurrentPeer.ID);


                                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children[k]).Content).PID = intPodId;
                                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children[k]).Content).PageId = intPageId.ToString();
                                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children[k]).Content).TabId = intPageId.ToString() + "," + intTabId.ToString();
                                                        }
                                                    }
                                                }

                                                #region Create Message

                                                List<clsModuleInfo> lstModuleInfo = new List<clsModuleInfo>();

                                                for (int tCnt = 0; tCnt < ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items.Count; tCnt++)
                                                {
                                                    if (((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Header.ToString() != "+")
                                                    {
                                                        for (int pCnt = 0; pCnt < ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children.Count; pCnt++)
                                                        {
                                                            clsModuleInfo cmi = new clsModuleInfo();

                                                            cmi.intModuleId = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).ModID;
                                                            cmi.strModuleName = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).Text;
                                                            cmi.ModPermissions = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).ModPer;


                                                            cmi.strUri = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).WCFUri;
                                                            cmi.strPodid = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).PID.ToString();


                                                            cmi.strPageid = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).PageId;
                                                            cmi.strTabid = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).TabId;

                                                            cmi.strDropType = "Page Type";

                                                            lstModuleInfo.Add(cmi);
                                                        }
                                                    }
                                                }

                                                #endregion

                                                #region Send Message To Peer/s

                                                for (int i = 0; i < ((ClsTabsTabItem)e.Source).lstBuddyDragged.Count; i++)
                                                {
                                                    if (((ClsTabsTabItem)e.Source).lstBuddyDragged[i] == ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption)
                                                    {
                                                        flag = true;
                                                        break;
                                                    }
                                                }


                                                if (!flag)
                                                {
                                                    string droppedBName = ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption;

                                                    //Add dropped username in list of Page
                                                    ((ClsTabsTabItem)e.Source).lstBuddyDragged.Add(droppedBName);                                                    

                                                    //Add dropped username in combobox of Page
                                                    ComboItem cbiPageBuddy = new ComboItem();
                                                    cbiPageBuddy.setName(droppedBName);
                                                    ((ClsTabsTabItem)e.Source).cmbTabBuddy.Items.Add(cbiPageBuddy);

                                                    //Add dropped username in related Tab's combobox and list 
                                                    for (int tCnt = 0; tCnt < ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items.Count; tCnt++)
                                                    {
                                                        if (((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Header.ToString() != "+")
                                                        {
                                                            //Add dropped username in related Tab's combobox
                                                            ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).lstBuddyDragged.Add(droppedBName);
                                                            ComboItem cbiTabBuddy = new ComboItem();
                                                            cbiTabBuddy.setName(droppedBName);

                                                            //Add dropped username in related Tab's list
                                                            ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).cmbTabBuddy.Items.Add(cbiTabBuddy);

                                                            for (int pCnt = 0; pCnt < ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children.Count; pCnt++)
                                                            {
                                                                //Add dropped username in POD's combobox
                                                                ComboItem cbiPodBuddy = new ComboItem();
                                                                cbiPodBuddy.setName(droppedBName);
                                                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).cmbPodBuddy.Items.Add(cbiPodBuddy);

                                                                //Add dropped username in POD's list
                                                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).lstUsersDropped.Add(droppedBName);
                                                            }
                                                        }
                                                    }                                                    

                                                    for (int lstCnt = 0; lstCnt < lstModuleInfo.Count; lstCnt++)
                                                    {
                                                        lstModuleInfo[lstCnt].lstUsersDropped = ((ClsTabsTabItem)e.Source).lstBuddyDragged;
                                                    }
                                                    if (EntChannelSetPageMsg != null)
                                                    {
                                                        EntChannelSetPageMsg(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption, "OPEN_PAGE", lstModuleInfo);
                                                    }
                                                }

                                                #endregion

                                                MessageBox.Show("Page is saved");
                                            }
                                            else
                                            {
                                                MessageBox.Show("Login to save the page");
                                            }
                                        }
                                        else if (r == MessageBoxResult.No)
                                        {
                                            //No                                           
                                            MessageBox.Show("Save the Page to Collaborate");
                                        }
                                        else
                                        {
                                            //Cancel
                                            return;

                                        }
                                    }

                                    #endregion

                                    #region If Page Saved, Check whether any UnSaved Tabs or Pods

                                    else
                                    {
                                        bool tabflag = true;
                                        bool podflag = true;

                                        string strpageid = ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).PageID.ToString();

                                        for (int j = 0; j < ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items.Count; j++)
                                        {
                                            if (((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).TabTitle != "+")
                                            {
                                                if (((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).TabID.Split(',')[1] == "0" || ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).TabID.Split(',')[1] == "-1")
                                                {
                                                    tabflag = false;
                                                    break;
                                                }
                                            }
                                        }

                                        if (!tabflag)
                                        {
                                            for (int tabcount = 0; tabcount < ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items.Count; tabcount++)
                                            {
                                                if (((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).TabTitle != "+")
                                                {
                                                    for (int podcount = 0; podcount < ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children.Count; podcount++)
                                                    {
                                                        if (((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).PID.ToString() == "0" || ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).PID.ToString() == "-1")
                                                        {
                                                            podflag = false;
                                                        }
                                                    }
                                                }
                                            }
                                        }

                                        if (podflag == false || tabflag == false)
                                        {
                                            MessageBoxResult r;
                                            r = MessageBox.Show("Do You want to save the unsaved Pods and Tabs", "Save Pods/Tabs", MessageBoxButton.YesNoCancel, MessageBoxImage.Question, MessageBoxResult.Yes);
                                            if (r == MessageBoxResult.Yes)
                                            {
                                                //save the unsaved pods and tabs

                                                if (tabflag == false)
                                                {
                                                    //save tabs and pods
                                                    for (int tabcount = 0; tabcount < ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items.Count; tabcount++)
                                                    {
                                                        if (((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).TabTitle != "+")
                                                        {
                                                            if (((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).TabID.Split(',')[1] == "0" || ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).TabID.Split(',')[1] == "-1")
                                                            {
                                                                int intTabid = new ClsTabLogic().Add_Tab(-1, ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).PageID, 1,
                                                                    ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).TabTitle,
                                                                    "",
                                                                    VMuktiAPI.VMuktiInfo.CurrentPeer.ID);



                                                                ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Tag = strpageid + "," + intTabid;
                                                                ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).TabTag = strpageid + "," + intTabid;
                                                                ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).TabID = strpageid + "," + intTabid;

                                                                for (int podcount = 0; podcount < ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children.Count; podcount++)
                                                                {
                                                                    if (((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).PID.ToString() == "0" || ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).PID.ToString() == "-1")
                                                                    {
                                                                        int intPodId = ClsPod.AddPod(-1, intTabid.ToString(),
                                                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).Text,
                                                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).Height,
                                                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).Width,
                                                                            double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).GetValue(Canvas.LeftProperty).ToString()),
                                                                            double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).GetValue(Canvas.TopProperty).ToString()),
                                                                            "",
                                                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).ModID,
                                                                            VMuktiAPI.VMuktiInfo.CurrentPeer.ID);


                                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).PID = intPodId;
                                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).PageId = strpageid;
                                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).TabId = strpageid + "," + intTabid.ToString();
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    //save pods
                                                    for (int tabcount = 0; tabcount < ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items.Count; tabcount++)
                                                    {
                                                        if (((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).TabTitle != "+")
                                                        {
                                                            string strTabid = ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).TabID.Split(',')[1];
                                                            for (int podcount = 0; podcount < ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children.Count; podcount++)
                                                            {
                                                                if (((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).PID.ToString() == "0" || ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).PID.ToString() == "-1")
                                                                {
                                                                    int intPodId = ClsPod.AddPod(-1, strTabid,
                                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).Text,
                                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).Height,
                                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).Width,
                                                                        double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).GetValue(Canvas.LeftProperty).ToString()),
                                                                        double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).GetValue(Canvas.TopProperty).ToString()),
                                                                        "",
                                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).ModID,
                                                                        VMuktiAPI.VMuktiInfo.CurrentPeer.ID);


                                                                    ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).PID = intPodId;
                                                                    ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).PageId = strpageid;
                                                                    ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).TabId = strpageid + "," + strTabid;
                                                                }
                                                                else if (((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).ResizePod)
                                                                {
                                                                    int intPodId = ClsPod.AddPod(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).PID, strTabid,
                                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).Text,
                                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).Height,
                                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).Width,
                                                                        double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).GetValue(Canvas.LeftProperty).ToString()),
                                                                        double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).GetValue(Canvas.TopProperty).ToString()),
                                                                        "",
                                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).ModID,
                                                                        VMuktiAPI.VMuktiInfo.CurrentPeer.ID);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }

                                                #region Create Message

                                                List<clsModuleInfo> lstModuleInfo = new List<clsModuleInfo>();

                                                for (int tCnt = 0; tCnt < ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items.Count; tCnt++)
                                                {
                                                    if (((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Header.ToString() != "+")
                                                    {
                                                        for (int pCnt = 0; pCnt < ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children.Count; pCnt++)
                                                        {
                                                            clsModuleInfo cmi = new clsModuleInfo();

                                                            cmi.intModuleId = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).ModID;
                                                            cmi.strModuleName = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).Text;
                                                            cmi.ModPermissions = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).ModPer;


                                                            cmi.strUri = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).WCFUri;
                                                            cmi.strPodid = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).PID.ToString();


                                                            cmi.strPageid = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).PageId;
                                                            cmi.strTabid = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).TabId;

                                                            cmi.strDropType = "Page Type";

                                                            lstModuleInfo.Add(cmi);
                                                        }
                                                    }
                                                }

                                                #endregion

                                                #region Send Message To Peer/s

                                                for (int i = 0; i < ((ClsTabsTabItem)e.Source).lstBuddyDragged.Count; i++)
                                                {
                                                    if (((ClsTabsTabItem)e.Source).lstBuddyDragged[i] == ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption)
                                                    {
                                                        flag = true;
                                                        break;
                                                    }
                                                }


                                                if (!flag)
                                                {
                                                    string droppedBName = ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption;

                                                    //Add dropped username in list of Page
                                                    ((ClsTabsTabItem)e.Source).lstBuddyDragged.Add(droppedBName);

                                                    //Add dropped username in combobox of Page
                                                    ComboItem cbiPageBuddy = new ComboItem();
                                                    cbiPageBuddy.setName(droppedBName);
                                                    ((ClsTabsTabItem)e.Source).cmbTabBuddy.Items.Add(cbiPageBuddy);

                                                    //Add dropped username in related Tab's combobox and list 
                                                    for (int tCnt = 0; tCnt < ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items.Count; tCnt++)
                                                    {
                                                        if (((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Header.ToString() != "+")
                                                        {
                                                            //Add dropped username in related Tab's combobox
                                                            ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).lstBuddyDragged.Add(droppedBName);
                                                            ComboItem cbiTabBuddy = new ComboItem();
                                                            cbiTabBuddy.setName(droppedBName);

                                                            //Add dropped username in related Tab's list
                                                            ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).cmbTabBuddy.Items.Add(cbiTabBuddy);

                                                            for (int pCnt = 0; pCnt < ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children.Count; pCnt++)
                                                            {
                                                                //Add dropped username in POD's combobox
                                                                ComboItem cbiPodBuddy = new ComboItem();
                                                                cbiPodBuddy.setName(droppedBName);
                                                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).cmbPodBuddy.Items.Add(cbiPodBuddy);

                                                                //Add dropped username in POD's list
                                                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).lstUsersDropped.Add(droppedBName);
                                                            }
                                                        }
                                                    } 
                                                    for (int lstCnt = 0; lstCnt < lstModuleInfo.Count; lstCnt++)
                                                    {
                                                        lstModuleInfo[lstCnt].lstUsersDropped = ((ClsTabsTabItem)e.Source).lstBuddyDragged;
                                                    }
                                                    if (EntChannelSetPageMsg != null)
                                                    {
                                                        EntChannelSetPageMsg(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption, "OPEN_PAGE", lstModuleInfo);
                                                    }
                                                }

                                                #endregion

                                            }
                                            else if (r == MessageBoxResult.No)
                                            {
                                                MessageBox.Show("Save the tabs and pods to collaborate");
                                            }
                                            else
                                            {
                                                return;
                                            }
                                        }

                                        else
                                        {
                                            for (int tabcount = 0; tabcount < ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items.Count; tabcount++)
                                            {
                                                if (((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).TabTitle != "+")
                                                {
                                                    string strTabid = ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).TabID.Split(',')[1];
                                                    for (int podcount = 0; podcount < ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children.Count; podcount++)
                                                    {
                                                        if (((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).ResizePod)
                                                        {
                                                            int intPodId = ClsPod.AddPod(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).PID, strTabid,
                                                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).Text,
                                                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).Height,
                                                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).Width,
                                                                double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).GetValue(Canvas.LeftProperty).ToString()),
                                                                double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).GetValue(Canvas.TopProperty).ToString()),
                                                                "",
                                                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[podcount]).Content).ModID,
                                                                VMuktiAPI.VMuktiInfo.CurrentPeer.ID);
                                                        }
                                                    }
                                                }
                                            }

                                            #region Create Message

                                            List<clsModuleInfo> lstModuleInfo = new List<clsModuleInfo>();

                                            for (int tCnt = 0; tCnt < ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items.Count; tCnt++)
                                            {
                                                if (((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Header.ToString() != "+")
                                                {
                                                    for (int pCnt = 0; pCnt < ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children.Count; pCnt++)
                                                    {
                                                        clsModuleInfo cmi = new clsModuleInfo();

                                                        cmi.intModuleId = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).ModID;
                                                        cmi.strModuleName = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).Text;
                                                        cmi.ModPermissions = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).ModPer;


                                                        cmi.strUri = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).WCFUri;
                                                        cmi.strPodid = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).PID.ToString();


                                                        cmi.strPageid = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).PageId;
                                                        cmi.strTabid = ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).TabId;

                                                        cmi.strDropType = "Page Type";

                                                        lstModuleInfo.Add(cmi);
                                                    }
                                                }
                                            }

                                            #endregion

                                            #region Send Message To Peer/s

                                            for (int i = 0; i < ((ClsTabsTabItem)e.Source).lstBuddyDragged.Count; i++)
                                            {
                                                if (((ClsTabsTabItem)e.Source).lstBuddyDragged[i] == ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption)
                                                {
                                                    flag = true;
                                                    break;
                                                }
                                            }


                                            if (!flag)
                                            {
                                                string droppedBName = ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption;

                                                //Add dropped username in list of Page
                                                ((ClsTabsTabItem)e.Source).lstBuddyDragged.Add(droppedBName);

                                                //Add dropped username in combobox of Page
                                                ComboItem cbiPageBuddy = new ComboItem();
                                                cbiPageBuddy.setName(droppedBName);
                                                ((ClsTabsTabItem)e.Source).cmbTabBuddy.Items.Add(cbiPageBuddy);

                                                //Add dropped username in related Tab's combobox and list 
                                                for (int tCnt = 0; tCnt < ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items.Count; tCnt++)
                                                {
                                                    if (((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Header.ToString() != "+")
                                                    {
                                                        //Add dropped username in related Tab's combobox
                                                        ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).lstBuddyDragged.Add(droppedBName);
                                                        ComboItem cbiTabBuddy = new ComboItem();
                                                        cbiTabBuddy.setName(droppedBName);

                                                        //Add dropped username in related Tab's list
                                                        ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).cmbTabBuddy.Items.Add(cbiTabBuddy);

                                                        for (int pCnt = 0; pCnt < ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children.Count; pCnt++)
                                                        {
                                                            //Add dropped username in POD's combobox
                                                            ComboItem cbiPodBuddy = new ComboItem();
                                                            cbiPodBuddy.setName(droppedBName);
                                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).cmbPodBuddy.Items.Add(cbiPodBuddy);

                                                            //Add dropped username in POD's list
                                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tCnt]).Content).Children[pCnt]).Content).lstUsersDropped.Add(droppedBName);
                                                        }
                                                    }
                                                } 
                                                for (int lstCnt = 0; lstCnt < lstModuleInfo.Count; lstCnt++)
                                                {
                                                    lstModuleInfo[lstCnt].lstUsersDropped = ((ClsTabsTabItem)e.Source).lstBuddyDragged;
                                                }
                                                if (EntChannelSetPageMsg != null)
                                                {
                                                    EntChannelSetPageMsg(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption, "OPEN_PAGE", lstModuleInfo);
                                                }
                                            }

                                            #endregion
                                        }
                                    }
                                    #endregion
                                }

                                //MessageBox.Show("Dragged On Page");
                            }
                        }
                    }
                }
            }
        }
        
        public void AddNewPage()
        {
            #region Loading the AddPage

            try
            {
                ClsTabsTabItem tti = new ClsTabsTabItem();
                tti.Header = "Add Page";
                tti.Background = Brushes.Transparent;
                tti.Foreground = Brushes.Black;
                tti.Loaded += new RoutedEventHandler(tti_Loaded);
                tti.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(tti_PreviewMouseLeftButtonDown);
                tti.Tag = "-1";
                tti.TabTag = "-1";
                tti.TabID = "-1";
                tti.TabTitle = "Add Page";

                #region PageDeclarations

                ClsPageTabControl ptcTabs = new ClsPageTabControl();
                //ptcTabs.Height = ptc.Height;
                //ptcTabs.Width = ptc.Width;
                ptcTabs.Tag = "-1";
                ptcTabs.PageID = -1;
                ptcTabs.PageTag = "-1";
                ptcTabs.PageTitle = "Add Page";
                ptcTabs.Background = Brushes.Transparent;
                ptcTabs.Foreground = Brushes.Black;
                ptcTabs.BorderBrush = Brushes.Transparent;

                #endregion

                tti.Content = ptcTabs;
                ptc.Items.Add(tti);
            }
            catch (Exception exp)
            {
                //MessageBox.Show("btnDynamic_Click " + exp.Message);
                exp.Data.Add("My Key", "AddNewPage()--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
            }


            #endregion
        }

        public ClsPageTabControl LoadTabControl(double svLoginWidth)
        {
            ptc.Background = Brushes.Transparent;
            ptc.BorderBrush = Brushes.Transparent;

            ptc.HorizontalAlignment = HorizontalAlignment.Stretch;
            ptc.VerticalAlignment = VerticalAlignment.Stretch;

            return ptc;
        }

        void ptc_SizeChanged(object sender, SizeChangedEventArgs e)
        {

        }

        public void LoadDynamicModules(clsModuleInfo cmi, string from)
        {
            OpenCollPage(cmi, from, cnvmainHeight, cnvmainWidth, svloginWidth, svbuddylistwidth);
        }

        bool dynCtlPod_EntSavePage()
        {
            if (VMuktiAPI.VMuktiInfo.CurrentPeer.ID.ToString() != null)
            {
                if (((ClsTabsTabItem)ptc.SelectedItem).Tag.ToString().Split(',')[0] == "0" || ((ClsTabsTabItem)ptc.SelectedItem).Tag.ToString().Split(',')[0] == "-1")
                {
                    MessageBoxResult r;
                    r = MessageBox.Show("Do you want to save this page?", "Save Page", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes);

                    if (r == MessageBoxResult.Yes)
                    {
                        SavePage();
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return true;
                }
            }
            else
            {
                MessageBox.Show("Login to save the page", "Login", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);
                return false;
            }
        }

        void dynCtlPod_EntMainPodMouseDown(double height, double width, double x, double y, string mstate)
        {
            if (newWidth < (width + x))
            {
                ((Canvas)((Resizer)dynCtlPod.Parent).Parent).Width = dynCtlPod.Width + x;
            }
            if (newHeight < (height + y))
            {
                ((Canvas)((Resizer)dynCtlPod.Parent).Parent).Height = dynCtlPod.Height + y;
            }
        }

        public void SavePage()
        {
            ClsPageLogic cpl = new ClsPageLogic();
            if (!(VMuktiAPI.VMuktiInfo.CurrentPeer.ID <= 0))
            {
                int intPageId = cpl.Add_Page(-1, ((ClsTabsTabItem)ptc.SelectedItem).Header.ToString(), "", true, VMuktiAPI.VMuktiInfo.CurrentPeer.ID);

                ((ClsTabsTabItem)ptc.SelectedItem).Tag = intPageId;
                ((ClsTabsTabItem)ptc.SelectedItem).TabID = intPageId.ToString();
                ((ClsTabsTabItem)ptc.SelectedItem).TabTag = intPageId.ToString();
                ((ClsPageTabControl)(((ClsTabsTabItem)ptc.SelectedItem).Content)).Tag = intPageId;
                ((ClsPageTabControl)(((ClsTabsTabItem)ptc.SelectedItem).Content)).PageID = intPageId;
                ((ClsPageTabControl)(((ClsTabsTabItem)ptc.SelectedItem).Content)).PageTag = intPageId.ToString();


                cpl.Page_Allocated(-1, intPageId, VMuktiAPI.VMuktiInfo.CurrentPeer.ID);


                for (int j = 0; j < ((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items.Count; j++)
                {
                    if (((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).TabTitle != "+")
                    {
                        int intTabId = new ClsTabLogic().Add_Tab(-1, intPageId, 1, ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).TabTitle, "", VMuktiAPI.VMuktiInfo.CurrentPeer.ID);
                        ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Tag = intPageId + "," + intTabId;
                        ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).TabTag = intPageId + "," + intTabId;
                        ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).TabID = intPageId + "," + intTabId;


                        //save the related pods 



                        for (int k = 0; k < ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Content).Children.Count; k++)

                        //for (int k = 0; k < ((Canvas)((ScrollViewer)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Content).Content).Children.Count; k++)
                        {

                            int intPodId = ClsPod.AddPod(-1, intTabId.ToString(),
                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Content).Children[k]).Content).Text,
                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Content).Children[k]).Content).Height,
                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Content).Children[k]).Content).Width,
                                double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Content).Children[k]).Content).GetValue(Canvas.LeftProperty).ToString()),
                                double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Content).Children[k]).Content).GetValue(Canvas.TopProperty).ToString()),
                                "",
                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Content).Children[k]).Content).ModID,
                                VMuktiAPI.VMuktiInfo.CurrentPeer.ID);


                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Content).Children[k]).Content).PID = intPodId;
                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Content).Children[k]).Content).PageId = intPageId.ToString();
                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Content).Children[k]).Content).TabId = intPageId.ToString() + "," + intTabId.ToString();



                        }
                    }
                }
                MessageBox.Show("Page is saved");
            }
            else
            {
                MessageBox.Show("Login to save the page");
            }
        }

        #region PlusTabItems Event Handlers

        void ttiPlus_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            try
            {
                if (intIslost == 1)
                {
                    string str = ((ClsTabsTabItem)sender).Header.ToString();
                    TextBox txtHead = new TextBox();

                    if (str != "+")
                        txtHead.Text = str;
                    else
                        txtHead.Text = "";

                    txtHead.SelectionStart = 0;
                    txtHead.SelectionLength = txtHead.Text.Length;
                    txtHead.Height = 20;
                    txtHead.Width = 70;

                    txtHead.Select(0, txtHead.SelectionLength);

                    ((ClsTabsTabItem)sender).Header = txtHead;
                    ((ClsTabsTabItem)sender).Margin = new Thickness(0);
                    ((ClsTabsTabItem)sender).Background = Brushes.Transparent;

                    txtHead.LostFocus += new RoutedEventHandler(txtHead_LostFocus);
                    txtHead.KeyDown += new KeyEventHandler(txtHead_KeyDown);

                    ((TextBox)((ClsTabsTabItem)sender).Header).Focus();

                    keybd_event(VK_Tab, 0, 0, 0);
                    keybd_event(VK_Tab, 0, KEYEVENTF_KEYUP, 0);
                    intIslost = 0;
                }
            }
            catch (Exception exp)
            {
                //MessageBox.Show("ttiplus_PreviewMouseLeftButtonDown " + exp.Message);
                exp.Data.Add("My Key", "ttiPlus_PreviewMouseLeftButtonDown()--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
            }
        }

        void ttiPlus_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (((ClsTabsTabItem)sender).btnClose != null && (((ClsTabsTabItem)sender).Header.ToString() == "+" || ((ClsTabsTabItem)sender).Header.ToString() == "Add Page"))
                {
                    ((ClsTabsTabItem)sender).btnClose.Visibility = Visibility.Collapsed;
                }
                if (((ClsTabsTabItem)sender).btnRename != null && (((ClsTabsTabItem)sender).Header.ToString() == "+" || ((ClsTabsTabItem)sender).Header.ToString() == "Add Page"))
                {
                    ((ClsTabsTabItem)sender).btnRename.Visibility = Visibility.Collapsed;
                }
                if (((ClsTabsTabItem)sender).btnDelete != null && (((ClsTabsTabItem)sender).Header.ToString() == "+" || ((ClsTabsTabItem)sender).Header.ToString() == "Add Page"))
                {
                    ((ClsTabsTabItem)sender).btnDelete.Visibility = Visibility.Collapsed;
                }
                if (((ClsTabsTabItem)sender).cmbTabBuddy != null && (((ClsTabsTabItem)sender).Header.ToString() == "+" || ((ClsTabsTabItem)sender).Header.ToString() == "Add Page"))
                {
                    ((ClsTabsTabItem)sender).cmbTabBuddy.Visibility = Visibility.Collapsed;
                }

            }
            catch (Exception exp)
            {
                //MessageBox.Show("ttiPlus_Loaded " + exp.Message);
                exp.Data.Add("My Key", "ttiPlus_Loaded()--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);

            }
        }

        #endregion

        #region cnvNew Events Handlers

        void cnvNew_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            try
            {
                if (newHeight != e.NewSize.Height || newWidth != e.NewSize.Width)
                {
                    newHeight = e.NewSize.Height;
                    newWidth = e.NewSize.Width;
                    ((Canvas)sender).Width = e.NewSize.Width;
                    ((Canvas)sender).Height = e.NewSize.Height;
                }
            }
            catch (Exception exp)
            {
                //MessageBox.Show("cnvNew_SizeChanged " + exp.Message);
                exp.Data.Add("My Key", "cnvNew_SizeChanged()--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
            }
        }

        void cnvNew_Drop(object sender, DragEventArgs e)
        {
            try
            {
                #region ModuleDrag/Drop

                if (e.Data.GetDataPresent(typeof(CtlExpanderItem)))
                {
                    if (((CtlExpanderItem)e.Data.GetData(typeof(CtlExpanderItem))).Tag != null)
                    {
                        try
                        {
                            string[] strModid = ((CtlExpanderItem)e.Data.GetData(typeof(CtlExpanderItem))).Tag.ToString().Split(',');
                            if (strModid.Length > 1)
                            {
                                if (strModid[1] == "ModuleType")
                                {
                                    if (ptc.SelectedContent != null)
                                    {
                                        if (((ClsTabsTabItem)((ClsPageTabControl)ptc.SelectedContent).SelectedItem) != null)
                                        {
                                            try
                                            {
                                                //VMuktiAPI.VMuktiInfo.CurrentPeer.RoleID = 2;
                                                ClsPermissionCollection objCPC = ClsPermissionCollection.Get_PermissionRefModule(int.Parse(strModid[0]), VMuktiAPI.VMuktiInfo.CurrentPeer.RoleID);

                                                int[] arrPermissionValue = new int[objCPC.Count];

                                                for (int percount = 0; percount < objCPC.Count; percount++)
                                                {
                                                    arrPermissionValue[percount] = objCPC[percount].PermissionValue;
                                                }
                                                c = new CtlPod((int.Parse(strModid[0])), ((CtlExpanderItem)e.Data.GetData(typeof(CtlExpanderItem))).Caption, strModid[2], null, arrPermissionValue, false);
                                                //c.Height = 100.0;
                                                //c.Width = 200.0;
                                                c.ModID = (int.Parse(strModid[0]));
                                                c.Tag = "Host";
                                                c.ModPer = arrPermissionValue;
                                                c.PageId = ((ClsTabsTabItem)(((ClsPageTabControl)ptc.SelectedContent).Parent)).Tag.ToString();
                                                c.Text = ((CtlExpanderItem)e.Data.GetData(typeof(CtlExpanderItem))).Caption;
                                                c.SetValue(Canvas.LeftProperty, 5.0);
                                                c.SetValue(Canvas.TopProperty, 5.0);
                                                c.PID = 0;
                                                c.TabId = ((ClsTabsTabItem)((ClsPageTabControl)ptc.SelectedContent).SelectedItem).Tag.ToString();
                                                c.ResizePod = false;
                                                c.EntSavePage += new CtlPod.DelSavePage(c_EntSavePage);
                                                c.EntMainPodMouseDown += new CtlPod.DelMainPodMouseDown(c_EntMainPodMouseDown);
                                                c.EntCnvPodBodyDrop += new CtlPod.DelCnvPodBodyDrop(c_EntCnvPodBodyDrop);
                                                c.EntRemoveBuddy += new CtlPod.DelRemoveBuddy(c_EntRemoveBuddy);
                                                Resizer r = new Resizer();
                                                r.Content = c;
                                                r.SetValue(Canvas.LeftProperty, 5.0);
                                                r.SetValue(Canvas.TopProperty, 5.0);
                                                r.Background = Brushes.Transparent;
                                                r.SizeChanged += new SizeChangedEventHandler(r_SizeChanged);
                                                r.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(r_PreviewMouseLeftButtonDown);
                                                r.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(r_PreviewMouseLeftButtonUp);

                                                c.SizeChanged += new SizeChangedEventHandler(c_SizeChanged);

                                                c.PreviewMouseUp += new MouseButtonEventHandler(c_PreviewMouseUp);

                                                ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)ptc.SelectedContent).SelectedItem).Content).Children.Add(r);

                                                //((Canvas)((ScrollViewer)((ClsTabsTabItem)((ClsPageTabControl)ptc.SelectedContent).SelectedItem).Content).Content).Children.Add(r);

                                                r.IsGripEnabled = true;
                                                blnModuleflag = true;
                                            }

                                            catch (Exception exp)
                                            {

                                                //MessageBox.Show("ModuleDrag/Drop " + exp.Message);
                                                exp.Data.Add("My Key", "ModuleDrag/Drop()--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                                                ClsException.LogError(exp);
                                                ClsException.WriteToErrorLogFile(exp);
                                            }

                                        }
                                        else
                                        {
                                            MessageBox.Show("Select the page");

                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception exp)
                        {
                            //MessageBox.Show("cnvNew_Drop2" + exp.Message);
                            //if (exp.InnerException != null)
                            //{
                            //    MessageBox.Show(exp.InnerException.Message);
                            //}
                            exp.Data.Add("My Key", "cnvNew_Drop()-1--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                            ClsException.LogError(exp);
                            ClsException.WriteToErrorLogFile(exp);
                        }
                    }
                }
                #endregion
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "cnvNew_Drop()-2--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
            }
        }

        void c_EntRemoveBuddy(string from, List<string> to, string msg, clsModuleInfo cmi)
        {
            EntChannelRemoveDraggedBuddy(from, to, msg, cmi);
        }

        bool c_EntSavePage()
        {
            if (VMuktiAPI.VMuktiInfo.CurrentPeer.ID.ToString() != null)
            {
                if (((ClsTabsTabItem)ptc.SelectedItem).Tag.ToString().Split(',')[0] == "0" || ((ClsTabsTabItem)ptc.SelectedItem).Tag.ToString().Split(',')[0] == "-1")
                {
                    MessageBoxResult r;
                    r = MessageBox.Show("Do you want to save this page?", "Save Page", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes);
                    if (r == MessageBoxResult.Yes)
                    {
                        SavePage();
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    string newid = ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).TabID;
                    if (newid.Split(',')[1] == "0" || newid.Split(',')[1] == "-1")
                    {


                        int intTabId = new ClsTabLogic().Add_Tab(-1, int.Parse(newid.Split(',')[0]),
                            1, ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).TabTitle,
                            "", VMuktiAPI.VMuktiInfo.CurrentPeer.ID);
                        ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Tag = newid.Split(',')[0] + "," + intTabId;
                        ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).TabTag = newid.Split(',')[0] + "," + intTabId;
                        ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).TabID = newid.Split(',')[0] + "," + intTabId;


                        for (int j = 0; j < ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children.Count; j++)
                        {
                            int intPodId = ClsPod.AddPod(-1, intTabId.ToString(),
                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[j]).Content).Text,
                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[j]).Content).Height,
                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[j]).Content).Width,
                                double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[j]).Content).GetValue(Canvas.LeftProperty).ToString()),
                                double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[j]).Content).GetValue(Canvas.TopProperty).ToString()),
                                "",
                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[j]).Content).ModID,
                                VMuktiAPI.VMuktiInfo.CurrentPeer.ID);


                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[j]).Content).PID = intPodId;
                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[j]).Content).PageId = newid.Split(',')[0];
                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[j]).Content).TabId = newid.Split(',')[0] + "," + intTabId.ToString();


                        }

                        //((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Tag = intPageId + "," + intTabId;
                        //((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).TabTag = intPageId + "," + intTabId;
                        //((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).TabID = intPageId + "," + intTabId;


                        //save the related pods 



                        //        for (int k = 0; k < ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Content).Children.Count; k++)

                        //        //for (int k = 0; k < ((Canvas)((ScrollViewer)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Content).Content).Children.Count; k++)
                        //        {

                        //            int intPodId = ClsPod.AddPod(-1, intTabId.ToString(),
                        //                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Content).Children[k]).Content).Text,
                        //                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Content).Children[k]).Content).Height,
                        //                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Content).Children[k]).Content).Width,
                        //                double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Content).Children[k]).GetValue(Canvas.LeftProperty).ToString()),
                        //                double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Content).Children[k]).GetValue(Canvas.TopProperty).ToString()),
                        //                "",
                        //                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Content).Children[k]).Content).ModID,
                        //                VMuktiAPI.VMuktiInfo.CurrentPeer.ID);


                        //            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Content).Children[k]).Content).PID = intPodId;
                        //            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Content).Children[k]).Content).PageId = intPageId.ToString();
                        //            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).Items[j]).Content).Children[k]).Content).TabId = intPageId.ToString() + "," + intTabId.ToString();



                        //}

                    }
                    else
                    {
                        for (int k = 0; k < ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children.Count; k++)
                        {
                            if (((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[k]).Content).PID == null || ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[k]).Content).PID.ToString() == "0" || ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[k]).Content).PID.ToString() == "-1")
                            {

                                int intPodId = ClsPod.AddPod(-1, newid.Split(',')[1],
                               ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[k]).Content).Text,
                               ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[k]).Content).Height,
                               ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[k]).Content).Width,
                               double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[k]).Content).GetValue(Canvas.LeftProperty).ToString()),
                               double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[k]).Content).GetValue(Canvas.TopProperty).ToString()),
                               "",
                               ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[k]).Content).ModID,
                               VMuktiAPI.VMuktiInfo.CurrentPeer.ID);


                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[k]).Content).PID = intPodId;
                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[k]).Content).PageId = newid.Split(',')[0];
                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[k]).Content).TabId = newid.Split(',')[0] + "," + newid.Split(',')[1];

                                return true;

                            }
                        }
                    }
                    return true;
                }
            }
            else
            {
                MessageBox.Show("Login to save the page", "Login", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);
                return false;
            }
        }

        void c_EntCnvPodBodyDrop(string from, string to, string msg, VMukti.Business.CommonDataContracts.clsModuleInfo cmi)
        {
            EntChannelSetMsg(from, to, msg, cmi);
        }

        void c_EntMainPodMouseDown(double height, double width, double x, double y, string mState)
        {
            try
            {
                if (mState == "Pressed")
                {
                    if (Math.Round(((Canvas)((Resizer)c.Parent).Parent).Width) < Math.Round((width + x)))
                    {
                        ((Canvas)((Resizer)c.Parent).Parent).Width = Math.Round(((Canvas)((Resizer)c.Parent).Parent).Width + x);
                    }
                    if (Math.Round(((Canvas)((Resizer)c.Parent).Parent).Height) < Math.Round((height + y)))
                    {
                        ((Canvas)((Resizer)c.Parent).Parent).Height = Math.Round(((Canvas)((Resizer)c.Parent).Parent).Height + y);
                    }
                }
            }
            catch (Exception exp)
            {
            }
        }

        void cnvNew_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            try
            {
                if (((Canvas)sender).Tag != null)
                {
                    if (((Canvas)sender).Tag.GetType().ToString() == "VMukti.Presentation.MainPod.CtlPod")
                    {
                        if (((CtlPod)((Canvas)sender).Tag).mDown == true)
                        {
                            ((Resizer)((CtlPod)((Canvas)sender).Tag).Parent).SetValue(Canvas.LeftProperty, e.GetPosition((Canvas)sender).X - ((CtlPod)((Canvas)sender).Tag).TitleX);
                            ((Resizer)((CtlPod)((Canvas)sender).Tag).Parent).SetValue(Canvas.TopProperty, e.GetPosition((Canvas)sender).Y - ((CtlPod)((Canvas)sender).Tag).TitleY);

                            //((CtlPod)((Canvas)sender).Tag).ResizePod = true;
                            //CtlPod.blnPodResize = true;
                        }
                    }
                }
            }
            catch (Exception exp)
            {
                //MessageBox.Show(" cnvNew_MouseMove " + exp.Message);
                exp.Data.Add("My Key", "cnvNew_MouseMove--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
            }
        }

        #endregion

        #region TextBox

        void tb_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                if (((TextBox)sender).Text != "+")
                {
                    if (((TextBox)sender).Text != "")
                    {
                        if (((ClsTabsTabItem)((TextBox)sender).Parent).Content.ToString().Contains("PageTabControl"))
                        {
                            (((ClsPageTabControl)((ClsTabsTabItem)((TextBox)sender).Parent).Content)).PageTitle = ((TextBox)sender).Text.ToString();
                        }
                        else
                        {
                            ((ClsTabsTabItem)((TextBox)sender).Parent).TabTitle = ((TextBox)sender).Text.ToString();

                            //((ClsTabsTabItem)((TextBox)sender).Parent).Height = ((TextBox)sender).Height;
                            //((ClsTabsTabItem)((TextBox)sender).Parent).Width = ((TextBox)sender).Width;

                        }
                        ((TabItem)((TextBox)sender).Parent).Header = ((TextBox)sender).Text.ToString();
                    }
                    else
                    {
                        //MessageBox.Show("Enter the Title"); 

                        //((TextBox)sender).Focus();                     
                    }
                }
            }
            catch (Exception exp)
            {
                //MessageBox.Show("tb_LostFocus " + exp.Message);
                exp.Data.Add("My Key", "tb_LostFocus--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
            }
            finally
            {
                blnRename = false;
            }
        }

        void tb_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    TextBox t = sender as TextBox;


                     #region parsestring
                    int i, result = 0, result1 = 0;
                    i = Convert.ToInt16('z');
                    string compare = t.Text;
                    for (i = 65; i <= 90; i++)
                    {
                        char c = Convert.ToChar(i);
                        if (compare.Contains(c))
                        {
                            result = 1;
                            break;
                        }
                    }
                    if (result != 1)
                    {
                        for (i = 97; i <= 122; i++)
                        {
                            char c = Convert.ToChar(i);
                            if (compare.Contains(c))
                            {
                                result1 = 1;
                                break;
                            }
                        }
                    }
                    #endregion
                    if (result == 1 || result1 == 1)
                    {

                    if (t.Text.Trim() != "")
                    {
                        ((TabItem)t.Parent).Focus();
                    }
                    else
                    {
                        MessageBox.Show("Blank is not allowed as a title");
                    }
                }
                    else
                    {
                        MessageBox.Show("There Must be Some charecter in the name");
                    }

                }
                else if (e.Key != Key.Back && e.Key != Key.Delete && e.Key != Key.Up && e.Key != Key.Down && e.Key != Key.Left && e.Key != Key.Right)
                {
                    TextBox t = sender as TextBox;
                    if (t.Text.Length> 10)
                    {
                    t.Width += 5.8;
                    ((TabItem)t.Parent).Width += 5.8;
                    }

                }
                else if (e.Key == Key.Back || e.Key == Key.Delete)
                {
                    TextBox t = sender as TextBox;
                    if (t.Text.Length > 10 || t.Width >= 70)
                    {
                        t.Width -= 5.8;
                        ((TabItem)t.Parent).Width -= 5.8;
                    }
                    else
                    {
                        t.Width = 70;
                        ((TabItem)t.Parent).Width = 150;
                    }
                }
            }
            catch (Exception exp)
            {
                //MessageBox.Show("tb_PreviewKeyDown " + exp.Message);
                exp.Data.Add("My Key", "tb_PreviewKeyDown--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
            }
        }

        void txtHead_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    TextBox t = sender as TextBox;

                    #region parsestring
                    int i, result = 0, result1 = 0;
                    i = Convert.ToInt16('z');
                    string compare = t.Text;
                    for (i = 65; i <= 90; i++)
                    {
                        char c = Convert.ToChar(i);
                        if (compare.Contains(c))
                        {
                            result = 1;
                            break;
                        }
                    }
                    if (result != 1)
                    {
                        for (i = 97; i <= 122; i++)
                        {
                            char c = Convert.ToChar(i);
                            if (compare.Contains(c))
                            {
                                result1 = 1;
                                break;
                            }
                        }
                        if (result1 != 1)
                        {
                            for (i = 48; i <= 57; i++)
                            {
                                char c = Convert.ToChar(i);
                                if (compare.Contains(c))
                                {
                                    result1 = 1;
                                    break;
                                }
                            }
                        }
                    }
                    #endregion
                    if (result == 1 || result1 == 1)
                    {

                    if (t.Text.Trim() != "")
                    {
                        ((TabItem)t.Parent).Focus();
                    }
                    else
                    {
                        MessageBox.Show("Blank is not allowed as a title");
                    }
                }
                    else
                    {
                        MessageBox.Show("There Must be Some charecter in the name");
                    }

                }
                else if (e.Key != Key.Back && e.Key != Key.Delete && e.Key != Key.Up && e.Key != Key.Down && e.Key != Key.Left && e.Key != Key.Right)
                {
                    TextBox t = sender as TextBox;
                    
                    if (t.Text.Length > 10)
                    {
                    t.Width += 5.8;
                    ((TabItem)t.Parent).Width += 5.8;
                    }
                   

                }
                else if (e.Key == Key.Back || e.Key == Key.Delete)
                {
                    TextBox t = sender as TextBox;
                    if (t.Text.Length > 10 || t.Width >= 70)
                    {
                        t.Width -= 5.8;
                        ((TabItem)t.Parent).Width -= 5.8;
                    }
                    else
                    {
                        t.Width = 70;
                        ((TabItem)t.Parent).Width = 150;
                    }
                    //if (t.Width > 120)
                    //{
                    //    t.Width -= 5.8;
                    //    ((TabItem)t.Parent).Width -= 5.8;
                    //}
                    
                }



            }


            catch (Exception exp)
            {
                //MessageBox.Show("tb_PreviewKeyDown " + exp.Message);
                exp.Data.Add("My Key", "tb_PreviewKeyDown-1--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
            }

        }

        void txtHead_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                if (((TextBox)sender).Text != "+" && ((TextBox)sender).Text != "")
                {

                    Canvas cnvPlus = new Canvas();
                    cnvPlus.Background = Brushes.Transparent;
                    //cnvPlus.Height = svPlus.Height + 100;
                    //cnvPlus.Width = svPlus.Width + 100;
                    cnvPlus.AllowDrop = true;
                    cnvPlus.Drop += new DragEventHandler(cnvNew_Drop);
                    cnvPlus.MouseMove += new MouseEventHandler(cnvNew_MouseMove);
                    cnvPlus.SizeChanged += new SizeChangedEventHandler(cnvNew_SizeChanged);

                    ClsTabsTabItem ttiplus = new ClsTabsTabItem();
                    ttiplus.Header = "+";
                    ttiplus.Tag = ((ClsPageTabControl)((ClsTabsTabItem)((TextBox)sender).Parent).Parent).Tag.ToString() + ",-1";
                    ttiplus.TabID = ((ClsPageTabControl)((ClsTabsTabItem)((TextBox)sender).Parent).Parent).Tag.ToString() + ",-1";
                    ttiplus.TabTag = ((ClsPageTabControl)((ClsTabsTabItem)((TextBox)sender).Parent).Parent).Tag.ToString() + ",-1";
                    ttiplus.TabTitle = "+";

                    ttiplus.Background = Brushes.Transparent;
                    ttiplus.Foreground = Brushes.Black;
                    ttiplus.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(ttiPlus_PreviewMouseLeftButtonDown);
                    ttiplus.Loaded += new RoutedEventHandler(ttiPlus_Loaded);
                    ttiplus.Height = 20;
                    ttiplus.Width = 150;

                    ((ClsTabsTabItem)((TextBox)sender).Parent).Content = cnvPlus;
                    ((ClsPageTabControl)((ClsTabsTabItem)((TextBox)sender).Parent).Parent).Items.Add(ttiplus);
                    ((ClsTabsTabItem)((TextBox)sender).Parent).PreviewMouseLeftButtonDown -= new MouseButtonEventHandler(ttiPlus_PreviewMouseLeftButtonDown);
                    ((ClsTabsTabItem)((TextBox)sender).Parent).btnClose.Visibility = Visibility.Visible;
                    ((ClsTabsTabItem)((TextBox)sender).Parent).btnRename.Visibility = Visibility.Visible;
                    ((ClsTabsTabItem)((TextBox)sender).Parent).btnDelete.Visibility = Visibility.Visible;
                    ((ClsTabsTabItem)((TextBox)sender).Parent).TabTitle = ((TextBox)sender).Text.ToString();
                    ((ClsTabsTabItem)((TextBox)sender).Parent).TabID = ((ClsPageTabControl)((ClsTabsTabItem)((TextBox)sender).Parent).Parent).Tag.ToString() + ",0";
                    ((ClsTabsTabItem)((TextBox)sender).Parent).Tag = ((ClsPageTabControl)((ClsTabsTabItem)((TextBox)sender).Parent).Parent).Tag.ToString() + ",0";
                    ((ClsTabsTabItem)((TextBox)sender).Parent).TabTag = ((ClsPageTabControl)((ClsTabsTabItem)((TextBox)sender).Parent).Parent).Tag.ToString() + ",0";
                    ((ClsTabsTabItem)((TextBox)sender).Parent).Header = ((TextBox)sender).Text.ToString();

                    intIslost = 1;
                }
            }
            catch (Exception exp)
            {
                //MessageBox.Show("txtHead_LostFocus " + exp.Message);
                exp.Data.Add("My Key", "txtHead_LostFocus--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
            }
        }

        void txtHead_KeyDown1(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    TextBox t = sender as TextBox;
                     #region parsestring
                    int i, result = 0, result1 = 0;
                    i = Convert.ToInt16('z');
                    string compare = t.Text;
                    for (i = 65; i <= 90; i++)
                    {
                        char c = Convert.ToChar(i);
                        if (compare.Contains(c))
                        {
                            result = 1;
                            break;
                        }
                    }
                    if (result != 1)
                    {
                        for (i = 97; i <= 122; i++)
                        {
                            char c = Convert.ToChar(i);
                            if (compare.Contains(c))
                            {
                                result1 = 1;
                                break;
                            }
                        }
                        if (result1 != 1)
                        {
                            for (i = 48; i <= 57; i++)
                            {
                                char c = Convert.ToChar(i);
                                if (compare.Contains(c))
                                {
                                    result1 = 1;
                                    break;
                                }
                            }
                        }
                    }
                    #endregion
                    if (result == 1 || result1 == 1)
                    {
                    if (t.Text.Trim() != "")
                    {
                        ((TabItem)t.Parent).Focus();
                    }
                    else
                    {
                        MessageBox.Show("Blank is not allowed as a title");
                    }
                }
                    else
                    {
                        MessageBox.Show("There Must be Some charecter in the name");
                    }
                }
                else if (e.Key != Key.Back && e.Key != Key.Delete && e.Key != Key.Up && e.Key != Key.Down && e.Key != Key.Left && e.Key != Key.Right)
                {
                    TextBox t = sender as TextBox;
                   if (t.Text.Length > 10)
                   {
                    t.Width += 5.8;
                    ((TabItem)t.Parent).Width += 5.8;
                   }
                  

                }
                else if (e.Key == Key.Back || e.Key == Key.Delete)
                {
                    TextBox t = sender as TextBox;
                    if (t.Text.Length > 10 || t.Width >= 70)
                    {
                        t.Width -= 5.8;
                        ((TabItem)t.Parent).Width -= 5.8;
                    }
                    else
                    {
                        t.Width = 70;
                        ((TabItem)t.Parent).Width = 150;
                    }
                   
                }
            }
            catch (Exception exp)
            {
                //MessageBox.Show("tb_PreviewKeyDown " + exp.Message);
                exp.Data.Add("My Key", "tb_PreviewKeyDown-3--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
            }

        }

        void txtHead_LostFocus1(object sender, RoutedEventArgs e)
        {
            if (((TextBox)e.Source).Text.Trim() != "Add Page" && ((TextBox)e.Source).Text != "")
            {
                #region Add Page
                ClsTabsTabItem tti = new ClsTabsTabItem();
                tti.Background = Brushes.Transparent;
                tti.Header = "Add Page";
                tti.Foreground = Brushes.Black;
                tti.Loaded += new RoutedEventHandler(tti_Loaded);
                tti.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(tti_PreviewMouseLeftButtonDown);
                tti.Tag = "-1";
                tti.TabTag = "-1";
                tti.TabID = "-1";

                #region PageDeclarations

                ClsPageTabControl ptcTabs = new ClsPageTabControl();
                //ptcTabs.Height = ((ClsPageTabControl)((ClsTabsTabItem)((TextBox)sender).Parent).Parent).Height;
                //ptcTabs.Width = ((ClsPageTabControl)((ClsTabsTabItem)((TextBox)sender).Parent).Parent).Width;
                ptcTabs.Tag = "-1";
                ptcTabs.PageID = -1;
                ptcTabs.PageTag = "-1";
                ptcTabs.PageTitle = "Add Page";
                ptcTabs.Background = Brushes.Transparent;
                ptcTabs.Foreground = Brushes.Black;
                ptcTabs.BorderBrush = Brushes.Transparent;
                tti.Content = ptcTabs;
                ((ClsPageTabControl)((ClsTabsTabItem)((TextBox)sender).Parent).Parent).Items.Add(tti);

                #endregion

                #endregion

                #region ScrollViewer+Canvas as container for Pods

                Canvas cnvNew = new Canvas();
                cnvNew.Background = Brushes.Transparent;
                //cnvNew.Height = sv.Height + 30;
                //cnvNew.Width = sv.Width + 100;
                cnvNew.AllowDrop = true;
                cnvNew.Drop += new DragEventHandler(cnvNew_Drop);
                cnvNew.SizeChanged += new SizeChangedEventHandler(cnvNew_SizeChanged);
                cnvNew.MouseMove += new MouseEventHandler(cnvNew_MouseMove);

                #endregion

                #region TabItem

                ClsTabsTabItem ttinew = new ClsTabsTabItem();
                ttinew.Header = "Tab1";
                ttinew.Tag = "-1,-1";
                ttinew.TabID = "-1,-1";
                ttinew.TabTitle = "Tab1";
                ttinew.TabTag = "-1,-1";

                ttinew.Background = Brushes.Transparent;
                ttinew.Foreground = Brushes.Black;
                ttinew.Height = 20;
                ttinew.Width = 150;
                ttinew.AllowDrop = true;
                ttinew.Drop += new DragEventHandler(ttinew_Drop);
                ttinew.Content = cnvNew;

                ((ClsPageTabControl)((ClsTabsTabItem)((TextBox)sender).Parent).Content).Items.Add(ttinew);

                #endregion

                #region Plus TabItem

                ClsTabsTabItem ttiPlus = new ClsTabsTabItem();
                ttiPlus.Header = "+";
                ttiPlus.Tag = "-1,-1";
                ttiPlus.TabID = "-1,-1";
                ttiPlus.TabTag = "-1,-1";
                ttiPlus.TabTitle = "+";

                ttiPlus.Background = Brushes.Transparent;
                ttiPlus.Foreground = Brushes.Black;
                ttiPlus.Height = 20;
                ttiPlus.Width = 150;
                ttiPlus.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(ttiPlus_PreviewMouseLeftButtonDown);
                ttiPlus.Loaded += new RoutedEventHandler(ttiPlus_Loaded);

                ((ClsPageTabControl)((ClsTabsTabItem)((TextBox)sender).Parent).Content).Items.Add(ttiPlus);


                ((ClsTabsTabItem)((TextBox)sender).Parent).PreviewMouseLeftButtonDown -= new MouseButtonEventHandler(tti_PreviewMouseLeftButtonDown);
                ((ClsTabsTabItem)((TextBox)sender).Parent).btnClose.Visibility = Visibility.Visible;
                ((ClsTabsTabItem)((TextBox)sender).Parent).btnRename.Visibility = Visibility.Visible;
                ((ClsTabsTabItem)((TextBox)sender).Parent).btnDelete.Visibility = Visibility.Visible;
                ((ClsTabsTabItem)((TextBox)sender).Parent).AllowDrop = true;
                ((ClsTabsTabItem)((TextBox)sender).Parent).Drop += new DragEventHandler(tti_Drop);
                ((ClsTabsTabItem)((TextBox)sender).Parent).TabTitle = ((TextBox)sender).Text.ToString();
                ((ClsTabsTabItem)((TextBox)sender).Parent).TabID = "-1";
                ((ClsTabsTabItem)((TextBox)sender).Parent).TabTag = "-1";
                ((ClsTabsTabItem)((TextBox)sender).Parent).Tag = "-1";

                intIslost = 1;

                #endregion

                ((ClsTabsTabItem)((TextBox)sender).Parent).Header = ((TextBox)sender).Text.ToString();
                //intIslost = 1;
            }
        }

        #endregion

        #region AddPage Events Handlers

        void tti_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (intIslost == 1)
                {
                    string str = ((ClsTabsTabItem)sender).Header.ToString();
                    TextBox txtHead = new TextBox();

                    if (str != "+")
                        txtHead.Text = str;
                    else
                        txtHead.Text = "";

                    txtHead.SelectionStart = 0;
                    txtHead.SelectionLength = txtHead.Text.Length;
                    txtHead.Height = 20;
                    txtHead.Width = 70;

                    txtHead.Select(0, txtHead.SelectionLength);

                    ((ClsTabsTabItem)sender).Header = txtHead;
                    ((ClsTabsTabItem)sender).Height = 20;
                    ((ClsTabsTabItem)sender).Margin = new Thickness(0);
                    ((ClsTabsTabItem)sender).Background = Brushes.Transparent;

                    txtHead.LostFocus += new RoutedEventHandler(txtHead_LostFocus1);
                    txtHead.KeyDown += new KeyEventHandler(txtHead_KeyDown1);

                    ((TextBox)((ClsTabsTabItem)sender).Header).Focus();

                    keybd_event(VK_Tab, 0, 0, 0);
                    keybd_event(VK_Tab, 0, KEYEVENTF_KEYUP, 0);
                    intIslost = 0;
                }
            }
            catch (Exception exp)
            {
                //MessageBox.Show("ttiplus_PreviewMouseLeftButtonDown " + exp.Message);
                exp.Data.Add("My Key", "ttiplus_PreviewMouseLeftButtonDown--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
            }
        }

        void tti_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (((ClsTabsTabItem)sender).btnClose != null && (((ClsTabsTabItem)sender).Header.ToString() == "+" || ((ClsTabsTabItem)sender).Header.ToString() == "Add Page"))
                {
                    ((ClsTabsTabItem)sender).btnClose.Visibility = Visibility.Collapsed;
                }
                if (((ClsTabsTabItem)sender).btnRename != null && (((ClsTabsTabItem)sender).Header.ToString() == "+" || ((ClsTabsTabItem)sender).Header.ToString() == "Add Page"))
                {
                    ((ClsTabsTabItem)sender).btnRename.Visibility = Visibility.Collapsed;
                }
                if (((ClsTabsTabItem)sender).btnDelete != null && (((ClsTabsTabItem)sender).Header.ToString() == "+" || ((ClsTabsTabItem)sender).Header.ToString() == "Add Page"))
                {
                    ((ClsTabsTabItem)sender).btnDelete.Visibility = Visibility.Collapsed;
                }
                if (((ClsTabsTabItem)sender).cmbTabBuddy != null && (((ClsTabsTabItem)sender).Header.ToString() == "+" || ((ClsTabsTabItem)sender).Header.ToString() == "Add Page"))
                {
                    ((ClsTabsTabItem)sender).cmbTabBuddy.Visibility = Visibility.Collapsed;
                }
            }
            catch (Exception exp)
            {
                //MessageBox.Show("ttiPlus_Loaded " + exp.Message);
                exp.Data.Add("My Key", "ttiPlus_Loaded--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
            }
        }

        #endregion

        #region Close/RenameButtonHandlers

        public void tabClose(object sender, RoutedEventArgs e)
        {
            try
            {
                if (((ClsTabsTabItem)e.Source).Content.ToString().Contains("PageTabControl"))
                {
                    if (((ClsTabsTabItem)e.Source).Header.ToString().Trim() != "Add Page")
                    {
                        //save the page if not saved
                        string[] strTempId = ((ClsTabsTabItem)e.Source).Tag.ToString().Split(',');

                        ClsPageLogic cpl = new ClsPageLogic();


                        #region UnSaved Page

                        if (strTempId[0] == "-1" || strTempId[0] == "0")
                        {
                            try
                            {
                                if (!(VMuktiAPI.VMuktiInfo.CurrentPeer.ID <= 0))
                                {
                                    MessageBoxResult r;
                                    r = MessageBox.Show("Do you want to save this page?", "Save Page", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes);
                                    if (r == MessageBoxResult.Yes)
                                    {
                                        int intPageId = cpl.Add_Page(-1, ((ClsTabsTabItem)e.Source).Header.ToString(), "", true, VMuktiAPI.VMuktiInfo.CurrentPeer.ID);

                                        ((ClsTabsTabItem)e.Source).Tag = intPageId;
                                        ((ClsTabsTabItem)e.Source).TabID = intPageId.ToString();
                                        ((ClsTabsTabItem)e.Source).TabTag = intPageId.ToString();

                                        ((ClsPageTabControl)(((ClsTabsTabItem)e.Source).Content)).Tag = intPageId;
                                        ((ClsPageTabControl)(((ClsTabsTabItem)e.Source).Content)).PageID = intPageId;
                                        ((ClsPageTabControl)(((ClsTabsTabItem)e.Source).Content)).PageTag = intPageId.ToString();
                                        ((ClsPageTabControl)(((ClsTabsTabItem)e.Source).Content)).PageTitle = ((ClsTabsTabItem)e.Source).Header.ToString();


                                        cpl.Page_Allocated(-1, intPageId, VMuktiAPI.VMuktiInfo.CurrentPeer.ID);


                                        for (int j = 0; j < ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items.Count; j++)
                                        {
                                            if (((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).TabTitle != "+")
                                            {
                                                int intTabId = new ClsTabLogic().Add_Tab(-1, intPageId, 1, ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).TabTitle, "", VMuktiAPI.VMuktiInfo.CurrentPeer.ID);
                                                ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Tag = intPageId + "," + intTabId;
                                                ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).TabTag = intPageId + "," + intTabId;
                                                ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).TabID = intPageId + "," + intTabId;

                                                for (int k = 0; k < ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children.Count; k++)
                                                {

                                                    try
                                                    {
                                                        //MessageBox.Show("Text" + ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children[k]).Content).Text);
                                                        //MessageBox.Show("Left" + ((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children[k]).GetValue(Canvas.LeftProperty).ToString());
                                                        //MessageBox.Show("Top" + ((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children[k]).GetValue(Canvas.TopProperty).ToString());

                                                        int intPodId = ClsPod.AddPod(-1,
                                                        intTabId.ToString(),
                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children[k]).Content).Text,
                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children[k]).Content).Height,
                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children[k]).Content).Width,
                                                            //double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[k]).Content).GetValue(Canvas.LeftProperty).ToString()),
                                                            //double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[k]).Content).GetValue(Canvas.TopProperty).ToString()),


                                                         double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children[k]).GetValue(Canvas.LeftProperty).ToString()),
                                                         double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children[k]).GetValue(Canvas.TopProperty).ToString()),


                                                        //((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[k]).Content).TitleX,
                                                            //((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)ptc.SelectedItem).Content).SelectedItem).Content).Children[k]).Content).TitleY,

                                                        "",
                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children[k]).Content).ModID,
                                                         VMuktiAPI.VMuktiInfo.CurrentPeer.ID);

                                                        //((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children[k]).Content).btnMinimized_Click(null, null);
                                                        //VMuktiAPI.VMuktiHelper.CallEvent("SignOutFlag", null, null);
                                                    }
                                                    catch (Exception exp)
                                                    {
                                                        exp.Data.Add("My Key", "Add Pod");
                                                        ClsException.LogError(exp);
                                                        //MessageBox.Show("Pod Save " + exp.Message);
                                                        //MessageBox.Show("k= " + k.ToString());
                                                        //MessageBox.Show("Text =" + ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[j]).Content).Children[k]).Content).Text);
                                                    }
                                                }
                                            }
                                        }
                                        MessageBox.Show("Page is saved");

                                        //for loop for signout flag-page

                                        TabItem tabItem = e.Source as TabItem;

                                        //for(int podCnt=0;podCnt<tabitem
                                        for (int tCnt = 0; tCnt < ((ClsPageTabControl)tabItem.Content).Items.Count; tCnt++)
                                        {
                                            if (((ClsTabsTabItem)((ClsPageTabControl)tabItem.Content).Items[tCnt]).Header.ToString() != "+")
                                            {
                                                int podCount = ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)tabItem.Content).Items[tCnt]).Content).Children.Count;

                                                for (int pCnt = 0; pCnt < podCount; pCnt++)
                                                {
                                                    ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)tabItem.Content).Items[tCnt]).Content).Children[0]).Content).btnMinimized_Click(null, null);
                                                }
                                            }
                                        }
                                        VMuktiAPI.VMuktiHelper.CallEvent("SignOutFlag", null, null);

                                        if (tabItem != null)
                                        {
                                            TabControl tabControl = tabItem.Parent as TabControl;
                                            if (tabControl != null)
                                                tabControl.Items.Remove(tabItem);
                                        }

                                    }
                                    else
                                    {
                                        //for loop for signout flag-page

                                        TabItem tabItem = e.Source as TabItem;

                                        for (int tCnt = 0; tCnt < ((ClsPageTabControl)tabItem.Content).Items.Count; tCnt++)
                                        {
                                            if (((ClsTabsTabItem)((ClsPageTabControl)tabItem.Content).Items[tCnt]).Header.ToString() != "+")
                                            {
                                                int podCount = ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)tabItem.Content).Items[tCnt]).Content).Children.Count;

                                                for (int pCnt = 0; pCnt < podCount; pCnt++)
                                                {
                                                    ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)tabItem.Content).Items[tCnt]).Content).Children[0]).Content).btnMinimized_Click(null, null);
                                                }
                                            }
                                        }
                                        VMuktiAPI.VMuktiHelper.CallEvent("SignOutFlag", null, null);


                                        if (tabItem != null)
                                        {
                                            TabControl tabControl = tabItem.Parent as TabControl;
                                            if (tabControl != null)
                                                tabControl.Items.Remove(tabItem);

                                        }
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("Login to save the page");
                                }
                            }
                            catch (Exception exp)
                            {
                                MessageBox.Show("Unsaved page" + exp.Message);
                            }
                        }

                        #endregion


                        #region Saved Page
                        else
                        {
                            bool blnRemovePage = false;

                            for (int i = 0; i < ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items.Count; i++)
                            {
                                if (((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Header.ToString().Trim() != "+")
                                {
                                    string[] strTemp = ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).TabID.Split(',');

                                    if (strTemp[1] == "0" || strTemp[1] == "-1")
                                    {
                                        MessageBoxResult r;
                                        r = MessageBox.Show("Do you want to save this tab?", "Save Tab", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes);
                                        if (r == MessageBoxResult.Yes)
                                        {
                                            if (!(VMuktiAPI.VMuktiInfo.CurrentPeer.ID <= 0))
                                            {
                                                //MessageBox.Show("save tabs for pages");
                                                if (((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).TabTitle != "+")
                                                {
                                                    int intTabId = new ClsTabLogic().Add_Tab(-1,
                                                    ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).PageID,
                                                     1,
                                                    ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).TabTitle,
                                                    "",
                                                    VMuktiAPI.VMuktiInfo.CurrentPeer.ID);


                                                    ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).TabID = ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).PageID + "," + intTabId;
                                                    ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).TabTag = ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).PageID + "," + intTabId;
                                                    ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Tag = ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).PageID + "," + intTabId;


                                                    for (int k = 0; k < ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children.Count; k++)
                                                    {

                                                        // string strsplit=((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[k]).TabID.Split(',')[1];


                                                        //if(((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[k]
                                                        ClsPod.AddPod(-1,
                                                        intTabId.ToString(),

                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).Text,
                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).Height,
                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).Width,
                                                            //double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).GetValue(Canvas.LeftProperty).ToString()),
                                                            //double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).GetValue(Canvas.TopProperty).ToString()),

                                                        double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).GetValue(Canvas.LeftProperty).ToString()),
                                                        double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).GetValue(Canvas.TopProperty).ToString()),

                                                        "",
                                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).ModID,
                                                        VMuktiAPI.VMuktiInfo.CurrentPeer.ID);

                                                        //((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).btnMinimized_Click(null, null);
                                                        //VMuktiAPI.VMuktiHelper.CallEvent("SignOutFlag", null, null);
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                MessageBox.Show("Login to save the pages");
                                                break;
                                            }
                                        }
                                        else
                                        {
                                            //remove the page
                                            TabItem tabItem = e.Source as TabItem;
                                            if (tabItem != null)
                                            {
                                                TabControl tabControl = tabItem.Parent as TabControl;
                                                if (tabControl != null)
                                                    tabControl.Items.Remove(tabItem);
                                            }
                                            blnRemovePage = true;
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        //check if any pods unsaved 
                                        for (int k = 0; k < ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children.Count; k++)
                                        {
                                            //if(((Canvas)((ClsTabsTabItem)e.Source).Content).Children.Count[k].
                                            if (((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).PID == -1 || ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).PID == 0)
                                            {
                                                ClsPod.AddPod(-1,
                                                    ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).TabID.Split(',')[1].ToString(),
                                                    //intTabId.ToString(),

                                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).Text,
                                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).Height,
                                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).Width,
                                                    //double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).GetValue(Canvas.LeftProperty).ToString()),
                                                    //double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).GetValue(Canvas.TopProperty).ToString()),
                                                double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).GetValue(Canvas.LeftProperty).ToString()),
                                                double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).GetValue(Canvas.TopProperty).ToString()),
                                                "",
                                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).ModID,
                                                VMuktiAPI.VMuktiInfo.CurrentPeer.ID);

                                                //((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).btnMinimized_Click(null, null);
                                                //VMuktiAPI.VMuktiHelper.CallEvent("SignOutFlag", null, null);
                                            }
                                            else
                                            {

                                                if (((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).ResizePod)
                                                {
                                                    ClsPod.AddPod(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).PID,
                                                   ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).TabID.Split(',')[1].ToString(),
                                                   ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).Text,
                                                   ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).Height,
                                                   ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).Width,
                                                        //double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).GetValue(Canvas.LeftProperty).ToString()),
                                                        //double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).GetValue(Canvas.TopProperty).ToString()),

                                                   double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).GetValue(Canvas.LeftProperty).ToString()),
                                                   double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).GetValue(Canvas.TopProperty).ToString()),



                                                   "",
                                                   ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).ModID,
                                                   VMuktiAPI.VMuktiInfo.CurrentPeer.ID);

                                                    //((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[i]).Content).Children[k]).Content).btnMinimized_Click(null, null);
                                                    //VMuktiAPI.VMuktiHelper.CallEvent("SignOutFlag", null, null);
                                                }
                                            }
                                        }
                                    }
                                }
                            }





                            if (!blnRemovePage)
                            {
                                //call signout event ( the code below needs to be checked)                               
                                for (int tabcount = 0; tabcount < ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items.Count; tabcount++)
                                {
                                    if (((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Header.ToString() != "+")
                                    {
                                        int pCnt = ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children.Count;

                                        for (int pcount = 0; pcount < pCnt; pcount++)
                                        {
                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Content).Items[tabcount]).Content).Children[0]).Content).btnMinimized_Click(null, null);

                                        }
                                    }
                                }
                                VMuktiAPI.VMuktiHelper.CallEvent("SignOutFlag", null, null);
                                TabItem tabItem = e.Source as TabItem;
                                if (tabItem != null)
                                {
                                    TabControl tabControl = tabItem.Parent as TabControl;
                                    if (tabControl != null)
                                        tabControl.Items.Remove(tabItem);
                                }
                            }

                        }




                        #endregion

                    }

                }


                else
                {
                    //bool blnTabPodSave = false;

                    if (((ClsTabsTabItem)e.Source).TabTitle != "+")
                    {
                        string[] strTemp = ((ClsTabsTabItem)e.Source).Tag.ToString().Split(',');

                        //MessageBox.Show(strTemp[0]);
                        //MessageBox.Show(strTemp[1]);

                        if (strTemp[1] == "-1" || strTemp[1] == "0")
                        {
                            MessageBoxResult r;
                            r = MessageBox.Show("Do you want to save this Page?", "Save Page", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes);
                            if (r == MessageBoxResult.Yes)
                            {
                                if (strTemp[0] == "-1" || strTemp[0] == "0")
                                {
                                    //save page
                                    //ClsPageLogic cpl = new ClsPageLogic();
                                    if (!(VMuktiAPI.VMuktiInfo.CurrentPeer.ID <= 0))
                                    {
                                        ClsPageLogic cpl = new ClsPageLogic();

                                        int intPageId = cpl.Add_Page(-1, ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Parent).TabTitle.ToString(), "", true, VMuktiAPI.VMuktiInfo.CurrentPeer.ID);

                                        ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Parent).Tag = intPageId;
                                        ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Parent).TabID = intPageId.ToString();
                                        ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Parent).TabTag = intPageId.ToString();
                                        ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Parent).TabTitle = ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Parent).TabTitle.ToString();


                                        ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).PageID = intPageId;
                                        ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).PageTag = intPageId.ToString();
                                        ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Tag = intPageId;
                                        ((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).PageTitle = ((ClsTabsTabItem)((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Parent).TabTitle.ToString();

                                        cpl.Page_Allocated(-1, intPageId, VMuktiAPI.VMuktiInfo.CurrentPeer.ID);
                                        //strTemp[0] = intPageId.ToString();

                                        //save tab
                                        int intTabId = new ClsTabLogic().Add_Tab(-1, intPageId, 1, ((ClsTabsTabItem)e.Source).TabTitle, "", VMuktiAPI.VMuktiInfo.CurrentPeer.ID);

                                        ((ClsTabsTabItem)e.Source).TabID = intPageId + "," + intTabId.ToString();

                                        ((ClsTabsTabItem)e.Source).TabTag = intPageId + "," + intTabId.ToString();
                                        ((ClsTabsTabItem)e.Source).Tag = intPageId + "," + intTabId.ToString();
                                        //((ClsTabsTabItem)e.Source).TabTitle = ((ClsTabsTabItem)e.Source).TabTitle;

                                        for (int k = 0; k < ((Canvas)((ClsTabsTabItem)e.Source).Content).Children.Count; k++)
                                        {

                                            //for (int k = 0; k < ((Canvas)((ScrollViewer)((ClsTabsTabItem)e.Source).Content).Content).Children.Count; k++)
                                            //{
                                            ClsPod.AddPod(-1,
                                            intTabId.ToString(),

                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[k]).Content).Text,
                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[k]).Content).Height,
                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[k]).Content).Width,
                                                //double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[k]).Content).GetValue(Canvas.LeftProperty).ToString()),
                                                //double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[k]).Content).GetValue(Canvas.TopProperty).ToString()),
                                              double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[k]).GetValue(Canvas.LeftProperty).ToString()),
                                              double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[k]).GetValue(Canvas.TopProperty).ToString()),
                                            "",
                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[k]).Content).ModID,
                                            VMuktiAPI.VMuktiInfo.CurrentPeer.ID);

                                            //MessageBox.Show("1");
                                            //((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[k]).Content).btnMinimized_Click(null, null);
                                            //VMuktiAPI.VMuktiHelper.CallEvent("SignOutFlag", null, null);
                                            //MessageBox.Show("2");

                                            //blnTabPodSave = true;
                                            //((CtlPod)((Resizer)((Canvas)((ScrollViewer)((ClsTabsTabItem)e.Source).Content).Content).Children[k]).Content).text
                                        }

                                        /*************************************/

                                        //check if only tab
                                        //if yes then remove the whole tab and page
                                        //else remove the only tab 

                                        if (((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Items.Count > 2)
                                        {

                                            TabItem tabItem = e.Source as TabItem;
                                            if (tabItem != null)
                                            {
                                                TabControl tabControl = tabItem.Parent as TabControl;
                                                if (tabControl != null)
                                                    tabControl.Items.Remove(tabItem);
                                            }
                                        }
                                        else
                                        {

                                            TabItem tabItem = e.Source as TabItem;

                                            int pCnt = ((Canvas)tabItem.Content).Children.Count;

                                            for (int tCnt = 0; tCnt < pCnt; tCnt++)
                                            {
                                                ((CtlPod)((Resizer)((Canvas)tabItem.Content).Children[0]).Content).btnMinimized_Click(null, null);
                                            }

                                            VMuktiAPI.VMuktiHelper.CallEvent("SignOutFlag", null, null);

                                            if (tabItem != null)
                                            {
                                                TabControl tabControl = tabItem.Parent as TabControl;
                                                if (tabControl != null)
                                                    tabControl.Items.Remove(tabItem);

                                                ptc.Items.Remove(tabControl.Parent);

                                            }
                                        }


                                    }
                                    else
                                    {
                                        MessageBox.Show("Login to save the page");
                                        //break;
                                    }
                                }


                                //page is saved
                                else
                                {
                                    //save tabs and pods 
                                    //check if only tab
                                    //if yes 
                                    //remove both page and tab
                                    //else 
                                    //remove related tab

                                    if (VMuktiAPI.VMuktiInfo.CurrentPeer.ID.ToString() != null || VMuktiAPI.VMuktiInfo.CurrentPeer.ID.ToString() != "")
                                    {

                                        int intTabId = new ClsTabLogic().Add_Tab(-1, int.Parse(strTemp[0]), 1, ((ClsTabsTabItem)e.Source).TabTitle, "", VMuktiAPI.VMuktiInfo.CurrentPeer.ID);

                                        for (int podcount = 0; podcount < ((Canvas)((ClsTabsTabItem)e.Source).Content).Children.Count; podcount++)
                                        {
                                            ClsPod.AddPod(-1,
                                          intTabId.ToString(),

                                          ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).Text,
                                          ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).Height,
                                          ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).Width,
                                                //double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).GetValue(Canvas.LeftProperty).ToString()),
                                                //double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).GetValue(Canvas.TopProperty).ToString()),
                                          double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).GetValue(Canvas.LeftProperty).ToString()),
                                          double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).GetValue(Canvas.TopProperty).ToString()),
                                          "",
                                          ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).ModID,
                                          VMuktiAPI.VMuktiInfo.CurrentPeer.ID);
                                            //MessageBox.Show("3");
                                            //((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).btnMinimized_Click(null, null);
                                            //VMuktiAPI.VMuktiHelper.CallEvent("SignOutFlag", null, null);
                                            //MessageBox.Show("4");

                                        }



                                        if (((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Items.Count > 2)
                                        {

                                            TabItem tabItem = e.Source as TabItem;

                                            int pCnt = ((Canvas)tabItem.Content).Children.Count;

                                            for (int podCount = 0; podCount < pCnt; podCount++)
                                            {
                                                ((CtlPod)((Resizer)((Canvas)tabItem.Content).Children[0]).Content).btnMinimized_Click(null, null);
                                            }

                                            if (tabItem != null)
                                            {
                                                TabControl tabControl = tabItem.Parent as TabControl;
                                                if (tabControl != null)
                                                    tabControl.Items.Remove(tabItem);
                                            }
                                        }
                                        else
                                        {


                                            TabItem tabItem = e.Source as TabItem;

                                            int pCnt = ((Canvas)tabItem.Content).Children.Count;

                                            for (int podCount = 0; podCount < pCnt; podCount++)
                                            {
                                                ((CtlPod)((Resizer)((Canvas)tabItem.Content).Children[0]).Content).btnMinimized_Click(null, null);
                                            }
                                            if (tabItem != null)
                                            {
                                                TabControl tabControl = tabItem.Parent as TabControl;
                                                if (tabControl != null)
                                                    tabControl.Items.Remove(tabItem);

                                                ptc.Items.Remove(tabControl.Parent);

                                            }
                                        }
                                    }
                                    else
                                    {
                                        MessageBox.Show("Login to save the page");
                                        //break;
                                    }

                                }

                            }
                            else
                            {
                                TabItem tabItem1 = e.Source as TabItem;
                                if (tabItem1 != null)
                                {
                                    TabControl tabControl1 = tabItem1.Parent as TabControl;
                                    if (tabControl1 != null)
                                        tabControl1.Items.Remove(tabItem1);

                                    //ptc.Items.Remove(tabControl1.Parent);
                                }

                                //remove the tab and page
                            }
                        }
                        else
                        {

                            MessageBoxResult r;
                            r = MessageBox.Show("Do you want to save this Tab?", "Save Tab", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes);
                            if (r == MessageBoxResult.Yes)
                            {
                                if (VMuktiAPI.VMuktiInfo.CurrentPeer.ID.ToString() != null || VMuktiAPI.VMuktiInfo.CurrentPeer.ID.ToString() != "")
                                {
                                    //check if any unsaved pods
                                    //if yes then save the pod to database 
                                    //check if it is the only tab
                                    //if yes 
                                    //remove whole page with tab
                                    //else
                                    //remove only the tab
                                    //else no unsaved pods
                                    //check if it is the only tab
                                    //if yes 
                                    //remove whole page with tab
                                    //else
                                    //remove only the tab

                                    for (int podcount = 0; podcount < ((Canvas)((ClsTabsTabItem)e.Source).Content).Children.Count; podcount++)
                                    {
                                        if (((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).PID == -1 || ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).PID == 0)
                                        {
                                            int intPodId = ClsPod.AddPod(-1,
                                            ((ClsTabsTabItem)e.Source).TabTag.Split(',')[1].ToString(),
                                                //intTabId.ToString(),
                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).Text,
                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).Height,
                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).Width,
                                                //double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).GetValue(Canvas.LeftProperty).ToString()),
                                                //double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).GetValue(Canvas.TopProperty).ToString()),
                                            double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).GetValue(Canvas.LeftProperty).ToString()),
                                            double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).GetValue(Canvas.TopProperty).ToString()),
                                            "",
                                            ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).ModID,
                                            VMuktiAPI.VMuktiInfo.CurrentPeer.ID);

                                            //MessageBox.Show("5");
                                            //((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).btnMinimized_Click(null, null);
                                            //VMuktiAPI.VMuktiHelper.CallEvent("SignOutFlag", null, null);
                                            //MessageBox.Show("6");

                                        }
                                        else
                                        {
                                            if (((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).ResizePod)
                                            {
                                                ClsPod.AddPod(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).PID,
                                                ((ClsTabsTabItem)e.Source).TabTag.Split(',')[1].ToString(),
                                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).Text,
                                               ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).Height,
                                               ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).Width,
                                                    //double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).GetValue(Canvas.LeftProperty).ToString()),
                                                    //double.Parse(((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).GetValue(Canvas.TopProperty).ToString()),

                                               double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).GetValue(Canvas.LeftProperty).ToString()),
                                               double.Parse(((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).GetValue(Canvas.TopProperty).ToString()),
                                               "",
                                               ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).ModID,
                                               VMuktiAPI.VMuktiInfo.CurrentPeer.ID);

                                                //MessageBox.Show("7");
                                                //((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[podcount]).Content).btnMinimized_Click(null, null);
                                                //VMuktiAPI.VMuktiHelper.CallEvent("SignOutFlag", null, null);
                                                //MessageBox.Show("8");

                                            }
                                        }
                                    }
                                    int pCnt = ((Canvas)((ClsTabsTabItem)e.Source).Content).Children.Count;
                                    for (int z = 0; z < pCnt; z++)
                                    {
                                        ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[0]).Content).btnMinimized_Click(null, null);

                                    }
                                    VMuktiAPI.VMuktiHelper.CallEvent("SignOutFlag", null, null);
                                    if (((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Items.Count > 2)
                                    {

                                        TabItem tabItem = e.Source as TabItem;
                                        if (tabItem != null)
                                        {
                                            TabControl tabControl = tabItem.Parent as TabControl;
                                            if (tabControl != null)
                                                tabControl.Items.Remove(tabItem);
                                        }
                                    }
                                    else
                                    {

                                        TabItem tabItem = e.Source as TabItem;
                                        if (tabItem != null)
                                        {
                                            TabControl tabControl = tabItem.Parent as TabControl;
                                            if (tabControl != null)
                                                tabControl.Items.Remove(tabItem);

                                            ptc.Items.Remove(tabControl.Parent);

                                        }
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("Login to save the page");
                                }


                            }

                            else
                            {

                                for (int z = 0; z < ((Canvas)((ClsTabsTabItem)e.Source).Content).Children.Count; z++)
                                {
                                    ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)e.Source).Content).Children[z]).Content).btnMinimized_Click(null, null);
                                    VMuktiAPI.VMuktiHelper.CallEvent("SignOutFlag", null, null);
                                }

                                TabItem tabItem2 = e.Source as TabItem;
                                if (tabItem2 != null)
                                {
                                    TabControl tabControl2 = tabItem2.Parent as TabControl;
                                    if (tabControl2 != null)
                                        tabControl2.Items.Remove(tabItem2);

                                    ptc.Items.Remove(tabControl2.Parent);

                                }
                            }

                        }
                        //if (blnTabPodSave)
                        //{
                        //    TabItem tabItem = e.Source as TabItem;
                        //    if (tabItem != null)
                        //    {
                        //        TabControl tabControl = tabItem.Parent as TabControl;
                        //        if (tabControl != null)
                        //            tabControl.Items.Remove(tabItem);
                        //    }
                        //}
                    }
                }
            }
            catch (Exception exp)
            {
                //MessageBox.Show("tabClose " + exp.Message);
                exp.Data.Add("My Key", "tabClose--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
            }
        }

        public void tabRename(object sender, RoutedEventArgs e)
        {
            if (!blnRename)
            {
                try
                {
                    blnRename = true;
                    TabItem tabItem = e.Source as TabItem;
                    if (tabItem != null)
                    {
                        TextBox tb = new TextBox();
                        string[] str = tabItem.Header.ToString().Split(':');
                        if (str.Length > 1)
                        {
                            tb.Text = str[str.Length - 1];
                        }
                        else
                        {
                            tb.Text = str[0];
                        }
                        tb.Background = Brushes.Transparent;
                        tb.SelectionStart = 0;
                        tb.SelectionLength = tb.Text.Length;
                        tb.Height = 20;
                      //  tb.Width = 140;
                        tb.Select(0, tb.SelectionLength);

                        tb.LostFocus += new RoutedEventHandler(tb_LostFocus);

                        tb.PreviewKeyDown += new KeyEventHandler(tb_PreviewKeyDown);

                        tabItem.Header = tb;
                        tabItem.Margin = new Thickness(0);
                        tabItem.Background = Brushes.Transparent;

                        tb.Focus();
                        keybd_event(VK_Tab, 0, 0, 0);
                        keybd_event(VK_Tab, 0, KEYEVENTF_KEYUP, 0);
                    }
                }
                catch (Exception exp)
                {
                    //MessageBox.Show("tabRename " + exp.Message);
                    exp.Data.Add("My Key", "tabRename--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                    ClsException.LogError(exp);
                    ClsException.WriteToErrorLogFile(exp);
                }
            }
        }

        void tb_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    TextBox t = sender as TextBox;
                    if (t.Width < 70 || t.Text.Length < 10)
                    {
                        t.Width = 70;
                        ((TabItem)t.Parent).Width = 150;
                    }
         
                    #region parsestring
                    int i, result = 0, result1 = 0;
                    i = Convert.ToInt16('z');
                    string compare = t.Text;
                    for (i = 65; i <= 90; i++)
                    {
                        char c = Convert.ToChar(i);
                        if (compare.Contains(c))
                        {
                            result = 1;
                            break;
                        }
                    }
                    if (result != 1)
                    {
                        for (i = 97; i <= 122; i++)
                        {
                            char c = Convert.ToChar(i);
                            if (compare.Contains(c))
                            {
                                result1 = 1;
                                break;
                            }
                        }
                        if (result1 != 1)
                        {
                            for (i = 48; i <= 57; i++)
                            {
                                char c = Convert.ToChar(i);
                                if (compare.Contains(c))
                                {
                                    result1 = 1;
                                    break;
                                }
                            }
                        }
                    }
                    #endregion
                    if (result == 1 || result1 == 1)
                    {

                    if (t.Text.Trim() != "")
                    {
                        ((TabItem)t.Parent).Focus();
                    }
                    else
                    {
                        MessageBox.Show("Blank is not allowed as a title");
                    }
                }
                    else
                    {
                        MessageBox.Show("There Must be Some charecter in the name");
                    }

                }
                else if (e.Key != Key.Back && e.Key != Key.Delete && e.Key != Key.Up && e.Key != Key.Down && e.Key != Key.Left && e.Key != Key.Right)
                {
                    
                    TextBox t = sender as TextBox;
                    if (t.Text.Length > 10)
                    {
                    t.Width += 5.8;
                    ((TabItem)t.Parent).Width += 5.8;
                    }

                }
                else if (e.Key == Key.Back || e.Key == Key.Delete)
                {
                    TextBox t = sender as TextBox;
                   
                   
                    if (t.Text.Length > 10 || t.Width>=70)
                    {
                        t.Width -= 5.8;
                        ((TabItem)t.Parent).Width -= 5.8;
                    }
                    else
                    {
                        t.Width = 70;
                        ((TabItem)t.Parent).Width = 150;
                    }
                }
            }
            catch (Exception exp)
            {
                //MessageBox.Show("tb_PreviewKeyDown " + exp.Message);
                exp.Data.Add("My Key", "tb_PreviewKeyDown--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
            }
        }

        public void tabDelete(object sender, RoutedEventArgs e)
        {
            try
            {
                ClsTabsTabItem tabItem = e.Source as ClsTabsTabItem;
                if (tabItem != null)
                {
                    if (((ClsPageTabControl)((ClsTabsTabItem)e.Source).Parent).Items.Count > 2)
                    {
                        bool bln = tabItem.TabID.ToString().Contains(",");
                        if (bln)
                        {
                            string[] strTemp = tabItem.TabID.ToString().Split(',');
                            //Remove the unsaved tab 
                            MessageBoxResult r;
                            r = MessageBox.Show("Do you really want to delete this tab?(All the modules in this tab would also be deleted)", "Delete Tab", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
                            if (r == MessageBoxResult.Yes)
                            {
                                if (strTemp[1] == "-1" || strTemp[1] == "0")
                                {
                                    ClsPageTabControl tabControl = tabItem.Parent as ClsPageTabControl;
                                    if (tabControl != null)
                                        tabControl.Items.Remove(tabItem);
                                }
                                //Remove the saved tab
                                else
                                {
                                    //delete it from database and then remove from tabcontrol

                                    ClsPod.Remove_PodTab(int.Parse(strTemp[1]));

                                    ClsTabLogic ctl = new ClsTabLogic();
                                    ctl.Remove_Tab(int.Parse(strTemp[1]));

                                    //send msg to other users of buddy leaving the conference---tab
                                    int pCnt = ((Canvas)tabItem.Content).Children.Count;

                                    for (int tCnt = 0; tCnt < pCnt; tCnt++)
                                    {
                                        ((CtlPod)((Resizer)((Canvas)tabItem.Content).Children[0]).Content).btnMinimized_Click(null, null);
                                    }


                                    ClsPageTabControl tabControl = tabItem.Parent as ClsPageTabControl;
                                    if (tabControl != null)
                                        tabControl.Items.Remove(tabItem);
                                }
                            }
                        }
                        else
                        {
                            string strPageId = tabItem.TabID.ToString();
                            MessageBoxResult r;
                            r = MessageBox.Show("Do you really want to delete this tab?(All the modules in this tab would also be deleted)", "Delete Tab", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
                            if (r == MessageBoxResult.Yes)
                            {
                                if (strPageId == "-1" || strPageId == "0")
                                {
                                    ClsPageTabControl tabControl = tabItem.Parent as ClsPageTabControl;
                                    if (tabControl != null)
                                        tabControl.Items.Remove(tabItem);
                                }
                                //Remove the saved page and its related tabs
                                else
                                {
                                    //delete it from database and then remove from control                               

                                    ClsTabLogic ctl = new ClsTabLogic();

                                    DataSet ds = ctl.Get_TabPage(int.Parse(strPageId));
                                    if (ds != null)
                                    {
                                        if (ds.Tables != null)
                                        {
                                            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                                            {
                                                //Remove all Pods on specific Tab
                                                ClsPod.Remove_PodTab(int.Parse(ds.Tables[0].Rows[i][0].ToString()));

                                                //Remove all Tabs on specific Page
                                                ctl.Remove_Tab(int.Parse(ds.Tables[0].Rows[i][0].ToString()));
                                            }
                                        }
                                    }

                                    ClsPageLogic cpl = new ClsPageLogic();
                                    cpl.Remove_PageAllocated(int.Parse(strPageId), VMuktiAPI.VMuktiInfo.CurrentPeer.ID);
                                    cpl.Remove_Page(int.Parse(strPageId));

                                    //send msg to other users of buddy leaving the conference---page

                                    for (int tCnt = 0; tCnt < ((ClsPageTabControl)tabItem.Content).Items.Count; tCnt++)
                                    {
                                        if (((ClsTabsTabItem)((ClsPageTabControl)tabItem.Content).Items[tCnt]).Header.ToString() != "+")
                                        {
                                            int podCount = ((Canvas)((ClsTabsTabItem)((ClsPageTabControl)tabItem.Content).Items[tCnt]).Content).Children.Count;

                                            for (int pCnt = 0; pCnt < podCount; pCnt++)
                                            {
                                                ((CtlPod)((Resizer)((Canvas)((ClsTabsTabItem)((ClsPageTabControl)tabItem.Content).Items[tCnt]).Content).Children[0]).Content).btnMinimized_Click(null, null);
                                            }
                                        }
                                    }



                                    ClsPageTabControl tabControl = tabItem.Parent as ClsPageTabControl;
                                    if (tabControl != null)
                                        tabControl.Items.Remove(tabItem);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (tabItem.Tag.ToString().Contains(','))
                        {
                            MessageBox.Show("Cannot remove the only tab");
                        }
                        else
                        {
                            MessageBox.Show("Cannot remove the only page");
                        }
                    }
                }
            }
            catch (Exception exp)
            {
                //MessageBox.Show("tabRename " + exp.Message);
                exp.Data.Add("My Key", "tabRename-2--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
            }
        }

        #endregion

        #region ResizerHandlers

        void r_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (IsResizing == 1)
                {
                    IsResizing = 0;
                }
            }
            catch (Exception exp)
            {
                //MessageBox.Show("r_PreviewMouseLeftButtonUp " + exp.Message);
                exp.Data.Add("My Key", "r_PreviewMouseLeftButtonUp--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
            }

        }

        void r_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            IsResizing = 0;
        }

        void r_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            try
            {
                ((CtlPod)((Resizer)sender).Content).Height = e.NewSize.Height;
                ((CtlPod)((Resizer)sender).Content).Width = e.NewSize.Width;

                if (IsResizing != -1)
                    IsResizing = 1;

            }
            catch (Exception exp)
            {
                //MessageBox.Show("r_SizeChanged " + exp.Message);
                exp.Data.Add("My Key", "r_SizeChanged--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
            }
        }

        #endregion

        #region ButtonHandlers

        public void LoadDynamicPage(string btnTag, string btnContent, double cnvMainHeight, double cnvMainWidth, double svLoginWidth, double svBuddyListWidth)
        {
            try
            {

                ptc.Items.Remove(ptc.Items[ptc.Items.Count - 1]);

                ClsTabsTabItem tti = new ClsTabsTabItem();
                tti.Header = btnContent;
                tti.Foreground = Brushes.Black;
                tti.Tag = btnTag;
                tti.TabTag = btnTag;
                tti.TabID = btnTag;
                tti.Background = Brushes.Transparent;
                tti.AllowDrop = true;
                tti.Drop += new DragEventHandler(tti_Drop);


                #region PageDeclarations

                ClsPageTabControl ptcTabs = new ClsPageTabControl();
                ptcTabs.Tag = btnTag;
                ptcTabs.PageID = int.Parse(btnTag);
                ptcTabs.PageTag = btnTag;
                ptcTabs.PageTitle = btnContent;
                //ptcTabs.Height = ptc.Height;
                //ptcTabs.Width = ptc.Width;
                ptcTabs.Background = Brushes.Transparent;
                ptcTabs.Foreground = Brushes.Black;
                ptcTabs.BorderBrush = Brushes.Transparent;
                ptcTabs.BorderThickness = new Thickness(0, 0, 0, 0);


                #endregion

                ClsTabCollection ctc = ClsTabCollection.GetAll(int.Parse(btnTag));

                if (ctc.Count > 0)
                {
                    for (int j = 0; j < ctc.Count; j++)
                    {
                        #region ScrollViewer+Canvas as container for Pods

                        Canvas cnvNew = new Canvas();
                        cnvNew.Background = Brushes.Transparent;
                        cnvNew.Height = ptcTabs.Height;
                        cnvNew.Width = ptcTabs.Width;




                        ////cnvNew.Height = sv.Height + 30;
                        ////cnvNew.Width = sv.Width + 100;
                        cnvNew.HorizontalAlignment = HorizontalAlignment.Stretch;
                        cnvNew.VerticalAlignment = VerticalAlignment.Stretch;
                        cnvNew.AllowDrop = true;
                        cnvNew.Drop += new DragEventHandler(cnvNew_Drop);
                        cnvNew.MouseMove += new MouseEventHandler(cnvNew_MouseMove);
                        cnvNew.SizeChanged += new SizeChangedEventHandler(cnvNew_SizeChanged);

                        #endregion

                        #region TabItem

                        ClsTabsTabItem ttinew = new ClsTabsTabItem();
                        ttinew.Header = ctc[j].TabTitle;
                        ttinew.Tag = btnTag + "," + ctc[j].TabId;
                        ttinew.TabID = btnTag + "," + ctc[j].TabId;
                        ttinew.TabTitle = ctc[j].TabTitle;
                        ttinew.TabTag = btnTag + "," + ctc[j].TabId;


                        ttinew.Background = Brushes.Transparent;
                        ttinew.Foreground = Brushes.Black;
                        ttinew.Height = 20;
                        ttinew.Width = 150;
                        ttinew.AllowDrop = true;
                        ttinew.Drop += new DragEventHandler(ttinew_Drop);
                        ttinew.Content = cnvNew;

                        DataSet ds = ClsPod.Get_PodTab(ctc[j].TabId);

                        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                        {
                            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                            {
                                ClsModuleLogic cml = new ClsModuleLogic().GetPodModule(int.Parse(ds.Tables[0].Rows[i]["ModuleID"].ToString()));

                                ClsPermissionCollection objCPC = ClsPermissionCollection.Get_PermissionRefModule(int.Parse(ds.Tables[0].Rows[i]["ModuleID"].ToString()), VMuktiAPI.VMuktiInfo.CurrentPeer.RoleID);

                                int[] arrPermissionValue = new int[objCPC.Count];

                                for (int percount = 0; percount < objCPC.Count; percount++)
                                {
                                    arrPermissionValue[percount] = objCPC[percount].PermissionValue;
                                }
                                c = new CtlPod(cml.ModuleId, cml.ModuleTitle, cml.IsCollaborative, null, arrPermissionValue, true);
                                c.Height = double.Parse(ds.Tables[0].Rows[i]["PodHeight"].ToString());
                                c.Width = double.Parse(ds.Tables[0].Rows[i]["PodWidth"].ToString());
                                c.DBHeight = c.Height;
                                c.DBWidth = c.Width;
                                c.ModID = cml.ModuleId;
                                c.Text = cml.ModuleTitle;
                                c.PageId = btnTag;
                                c.SetValue(Canvas.TopProperty, double.Parse(ds.Tables[0].Rows[i]["PodTop"].ToString()));
                                c.SetValue(Canvas.LeftProperty, double.Parse(ds.Tables[0].Rows[i]["PodLeft"].ToString()));
                                c.PID = int.Parse(ds.Tables[0].Rows[i]["ID"].ToString());
                                c.TabId = btnTag + "," + ctc[j].TabId;
                                c.EntSavePage += new CtlPod.DelSavePage(c_EntSavePage);
                                c.EntMainPodMouseDown += new CtlPod.DelMainPodMouseDown(c_EntMainPodMouseDown);
                                c.EntCnvPodBodyDrop += new CtlPod.DelCnvPodBodyDrop(c_EntCnvPodBodyDrop);
                                c.EntRemoveBuddy+=new CtlPod.DelRemoveBuddy(c_EntRemoveBuddy);
                                c.PreviewMouseUp += new MouseButtonEventHandler(c_PreviewMouseUp);
                                c.SizeChanged += new SizeChangedEventHandler(c_SizeChanged);
                                Resizer r = new Resizer();
                                r.SetValue(Canvas.LeftProperty, double.Parse(ds.Tables[0].Rows[i]["PodLeft"].ToString()));
                                r.SetValue(Canvas.TopProperty, double.Parse(ds.Tables[0].Rows[i]["PodTop"].ToString()));
                                c.DBTop = double.Parse(ds.Tables[0].Rows[i]["PodTop"].ToString());
                                c.DBLeft = double.Parse(ds.Tables[0].Rows[i]["PodLeft"].ToString());
                                r.Content = c;
                                r.Background = Brushes.Transparent;
                                r.SizeChanged += new SizeChangedEventHandler(r_SizeChanged);
                                r.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(r_PreviewMouseLeftButtonDown);
                                r.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(r_PreviewMouseLeftButtonUp);

                                cnvNew.Children.Add(r);

                            }
                        }

                        ptcTabs.Items.Add(ttinew);


                        #endregion
                    }
                }

                #region Plus TabItem

                Canvas cnvPlus = new Canvas();
                cnvPlus.Background = Brushes.Transparent;
                //cnvPlus.Height = svPlus.Height + 30;
                //cnvPlus.Width = svPlus.Width + 100;

                cnvPlus.AllowDrop = true;
                //cnvPlus.Drop += new DragEventHandler(cnvNew_Drop);
                //cnvPlus.MouseMove += new MouseEventHandler(cnvNew_MouseMove);
                //cnvPlus.SizeChanged += new SizeChangedEventHandler(cnvNew_SizeChanged);

                ClsTabsTabItem ttiPlus = new ClsTabsTabItem();
                ttiPlus.Header = "+";
                ttiPlus.Tag = btnTag + ",-1";
                ttiPlus.TabID = btnTag + ",-1";
                ttiPlus.TabTag = btnTag + ",-1";
                ttiPlus.TabTitle = "+";


                ttiPlus.Background = Brushes.Transparent;
                ttiPlus.Foreground = Brushes.Black;
                ttiPlus.Height = 20;
                ttiPlus.Width = 150;
                ttiPlus.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(ttiPlus_PreviewMouseLeftButtonDown);
                ttiPlus.Loaded += new RoutedEventHandler(ttiPlus_Loaded);

                ttiPlus.Content = cnvPlus;

                ptcTabs.Items.Add(ttiPlus);

                #endregion

                tti.Content = ptcTabs;
                ptc.Items.Add(tti);

                ((ClsTabsTabItem)ptc.Items[ptc.Items.Count - 1]).Focus();

                AddNewPage();

            }
            catch (Exception exp)
            {
                //MessageBox.Show("btnDynamic_Click " + exp.Message);
                exp.Data.Add("My Key", "btnDynamic_Click-1--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
            }
        }

        public void LoadDynamicPage(clsModuleInfo cmi, string from, double cnvMainHeight, double cnvMainWidth, double svLoginWidth, double svBuddyListWidth)
        {
            try
            {
                ptc.Items.Remove(ptc.Items[ptc.Items.Count - 1]);

                ClsPageLogic cpl = new ClsPageLogic().Get_PageInfo(int.Parse(cmi.strPageid));

                ClsTabsTabItem tti = new ClsTabsTabItem();
                tti.Header = cpl.PageTitle;
                tti.Foreground = Brushes.Black;
                tti.Tag = cmi.strPageid;
                tti.TabTag = cmi.strPageid;
                tti.TabID = cmi.strPageid;
                tti.Background = Brushes.Transparent;

                #region PageDeclarations

                ClsPageTabControl ptcTabs = new ClsPageTabControl();
                ptcTabs.Tag = cmi.strPageid;
                ptcTabs.PageID = int.Parse(cmi.strPageid);
                ptcTabs.PageTag = cmi.strPageid;
                ptcTabs.PageTitle = cpl.PageTitle;
                //ptcTabs.Height = ptc.Height;
                //ptcTabs.Width = ptc.Width;
                ptcTabs.Background = Brushes.Transparent;
                ptcTabs.Foreground = Brushes.Black;
                ptcTabs.BorderBrush = Brushes.Transparent;
                ptcTabs.BorderThickness = new Thickness(0, 0, 0, 0);


                #endregion

                ClsTabCollection ctc = ClsTabCollection.GetAll(int.Parse(cmi.strPageid));

                if (ctc.Count > 0)
                {
                    for (int j = 0; j < ctc.Count; j++)
                    {

                        #region ScrollViewer+Canvas as container for Pods

                        Canvas cnvNew = new Canvas();
                        cnvNew.Background = Brushes.Transparent;
                        cnvNew.Height = ptcTabs.Height;
                        cnvNew.Width = ptcTabs.Width;

                        ////cnvNew.Height = sv.Height + 30;
                        ////cnvNew.Width = sv.Width + 100;
                        cnvNew.HorizontalAlignment = HorizontalAlignment.Stretch;
                        cnvNew.VerticalAlignment = VerticalAlignment.Stretch;
                        cnvNew.AllowDrop = true;
                        cnvNew.Drop += new DragEventHandler(cnvNew_Drop);
                        cnvNew.MouseMove += new MouseEventHandler(cnvNew_MouseMove);
                        cnvNew.SizeChanged += new SizeChangedEventHandler(cnvNew_SizeChanged);

                        #endregion

                        #region TabItem

                        ClsTabsTabItem ttinew = new ClsTabsTabItem();
                        ttinew.Header = ctc[j].TabTitle;
                        ttinew.Tag = cmi.strPageid + "," + ctc[j].TabId;
                        ttinew.TabID = cmi.strPageid + "," + ctc[j].TabId;
                        ttinew.TabTitle = ctc[j].TabTitle;
                        ttinew.TabTag = cmi.strPageid + "," + ctc[j].TabId;

                        ttinew.Background = Brushes.Transparent;
                        ttinew.Foreground = Brushes.Black;
                        ttinew.Height = 20;
                        ttinew.Width = 150;
                        ttinew.Content = cnvNew;

                        DataSet ds = ClsPod.Get_PodTab(ctc[j].TabId);

                        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                        {
                            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                            {
                                ClsModuleLogic cml = new ClsModuleLogic().GetPodModule(int.Parse(ds.Tables[0].Rows[i]["ModuleID"].ToString()));

                                ClsPermissionCollection objCPC = ClsPermissionCollection.Get_PermissionRefModule(int.Parse(ds.Tables[0].Rows[i]["ModuleID"].ToString()), 1);

                                int[] arrPermissionValue = new int[objCPC.Count];

                                for (int percount = 0; percount < objCPC.Count; percount++)
                                {
                                    arrPermissionValue[percount] = objCPC[percount].PermissionValue;
                                }
                                if (VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.BootStrap || VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.SuperNode || VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.NodeWithNetP2P)
                                {
                                    c = new CtlPod(cmi.intModuleId, cmi.strModuleName, "True", cmi.strUri[0], cmi.ModPermissions, from);
                                }
                                else if (VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.NodeWithHttp)
                                {
                                    c = new CtlPod(cmi.intModuleId, cmi.strModuleName, "True", cmi.strUri[1], cmi.ModPermissions, from);
                                }
                                c.Height = double.Parse(ds.Tables[0].Rows[i]["PodHeight"].ToString());
                                c.Width = double.Parse(ds.Tables[0].Rows[i]["PodWidth"].ToString());
                                c.ModPer = cmi.ModPermissions;
                                c.DBHeight = c.Height;
                                c.DBWidth = c.Width;
                                c.ModID = cml.ModuleId;
                                c.Text = cml.ModuleTitle;
                                c.PageId = cmi.strPageid;
                                c.ResizePod = false;
                                c.PID = int.Parse(ds.Tables[0].Rows[i]["ID"].ToString());
                                c.TabId = cmi.strPageid + "," + ctc[j].TabId;
                                c.EntSavePage += new CtlPod.DelSavePage(dynCtlPod_EntSavePage);
                                c.EntMainPodMouseDown += new CtlPod.DelMainPodMouseDown(dynCtlPod_EntMainPodMouseDown);
                                c.EntCnvPodBodyDrop += new CtlPod.DelCnvPodBodyDrop(c_EntCnvPodBodyDrop);
                                c.EntRemoveBuddy+=new CtlPod.DelRemoveBuddy(c_EntRemoveBuddy);
                                //c.PreviewMouseUp += new MouseButtonEventHandler(c_PreviewMouseUp);
                                //c.SizeChanged += new SizeChangedEventHandler(c_SizeChanged);
                                Resizer r = new Resizer();
                                r.SetValue(Canvas.LeftProperty, double.Parse(ds.Tables[0].Rows[i]["PodLeft"].ToString()));
                                r.SetValue(Canvas.TopProperty, double.Parse(ds.Tables[0].Rows[i]["PodTop"].ToString()));
                                c.DBTop = double.Parse(ds.Tables[0].Rows[i]["PodTop"].ToString());
                                c.DBLeft = double.Parse(ds.Tables[0].Rows[i]["PodLeft"].ToString());
                                r.Content = c;
                                r.Background = Brushes.Transparent;
                                r.SizeChanged += new SizeChangedEventHandler(r_SizeChanged);
                                r.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(r_PreviewMouseLeftButtonDown);
                                r.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(r_PreviewMouseLeftButtonUp);
                                cnvNew.Children.Add(r);
                            }
                        }

                        ptcTabs.Items.Add(ttinew);


                        #endregion
                    }
                }

                #region Plus TabItem

                Canvas cnvPlus = new Canvas();
                cnvPlus.Background = Brushes.Transparent;
                //cnvPlus.Height = svPlus.Height + 30;
                //cnvPlus.Width = svPlus.Width + 100;

                cnvPlus.AllowDrop = true;
                //cnvPlus.Drop += new DragEventHandler(cnvNew_Drop);
                //cnvPlus.MouseMove += new MouseEventHandler(cnvNew_MouseMove);
                //cnvPlus.SizeChanged += new SizeChangedEventHandler(cnvNew_SizeChanged);

                ClsTabsTabItem ttiPlus = new ClsTabsTabItem();
                ttiPlus.Header = "+";
                ttiPlus.Tag = cmi.strPageid + ",-1";
                ttiPlus.TabID = cmi.strPageid + ",-1";
                ttiPlus.TabTag = cmi.strPageid + ",-1";
                ttiPlus.TabTitle = "+";


                ttiPlus.Background = Brushes.Transparent;
                ttiPlus.Foreground = Brushes.Black;
                ttiPlus.Height = 20;
                ttiPlus.Width = 150;
                ttiPlus.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(ttiPlus_PreviewMouseLeftButtonDown);
                ttiPlus.Loaded += new RoutedEventHandler(ttiPlus_Loaded);

                ttiPlus.Content = cnvPlus;

                ptcTabs.Items.Add(ttiPlus);

                #endregion

                tti.Content = ptcTabs;
                ptc.Items.Add(tti);

                ((ClsTabsTabItem)ptc.Items[ptc.Items.Count - 1]).Focus();

                AddNewPage();

            }
            catch (Exception exp)
            {
                //MessageBox.Show("btnDynamic_Click " + exp.Message);
                exp.Data.Add("My Key", "btnDynamic_Click-2--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
            }
        }

        void c_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (((CtlPod)sender).PID != 0 && ((CtlPod)sender).PID != -1)
            {

                if (((CtlPod)sender).DBTop != double.Parse(((Resizer)((CtlPod)e.Source).Parent).GetValue(Canvas.TopProperty).ToString()) || ((CtlPod)sender).DBLeft != double.Parse(((Resizer)((CtlPod)e.Source).Parent).GetValue(Canvas.LeftProperty).ToString()))
                {
                    ((CtlPod)sender).ResizePod = true;
                }
            }
        }

        void c_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (((CtlPod)sender).PID != 0 && ((CtlPod)sender).PID != -1)
            {
                ((CtlPod)sender).ResizePod = true;
            }
        }

        public void OpenCollPage(clsModuleInfo cmi, string from, double cnvMainHeight, double cnvMainWidth, double svLoginWidth, double svBuddyListWidth)
        {
            try
            {
                ptc.Items.Remove(ptc.Items[ptc.Items.Count - 1]);

                #region Pod Type

                if (cmi.strDropType == "Pod Type")
                {
                    ClsPageLogic cpl = new ClsPageLogic().Get_PageInfo(int.Parse(cmi.strPageid));


                    ClsTabsTabItem tti = new ClsTabsTabItem();
                    tti.Header = cpl.PageTitle;
                    tti.Foreground = Brushes.Black;
                    tti.Tag = cmi.strPageid;
                    tti.TabTag = cmi.strPageid;
                    tti.TabID = cmi.strPageid;
                    tti.Background = Brushes.Transparent;
                    //tti.lstBuddyDragged = cmi.lstUsersDropped;

                    #region PageDeclarations

                    ClsPageTabControl ptcTabs = new ClsPageTabControl();
                    ptcTabs.Tag = cmi.strPageid;
                    ptcTabs.PageID = int.Parse(cmi.strPageid);
                    ptcTabs.PageTag = cmi.strPageid;
                    ptcTabs.PageTitle = cpl.PageTitle;
                    //ptcTabs.Height = ptc.Height;
                    //ptcTabs.Width = ptc.Width;
                    ptcTabs.Background = Brushes.Transparent;
                    ptcTabs.Foreground = Brushes.Black;
                    ptcTabs.BorderBrush = Brushes.Transparent;
                    ptcTabs.BorderThickness = new Thickness(0, 0, 0, 0);

                    #endregion

                    string[] strTab = cmi.strTabid.Split(',');

                    ClsTabCollection ctc = ClsTabCollection.GetAll(int.Parse(cmi.strPageid));
                    for (int j = 0; j < ctc.Count; j++)
                    {
                        if (strTab.Length > 1)
                        {
                            if (ctc[j].TabId.ToString() == strTab[1])
                            {
                                #region ScrollViewer+Canvas as container for Pods

                                Canvas cnvNew = new Canvas();
                                cnvNew.Background = Brushes.Transparent;
                                cnvNew.Height = ptcTabs.Height;
                                cnvNew.Width = ptcTabs.Width;

                                ////cnvNew.Height = sv.Height + 30;
                                ////cnvNew.Width = sv.Width + 100;
                                cnvNew.HorizontalAlignment = HorizontalAlignment.Stretch;
                                cnvNew.VerticalAlignment = VerticalAlignment.Stretch;
                                cnvNew.AllowDrop = true;
                                cnvNew.Drop += new DragEventHandler(cnvNew_Drop);
                                cnvNew.MouseMove += new MouseEventHandler(cnvNew_MouseMove);
                                cnvNew.SizeChanged += new SizeChangedEventHandler(cnvNew_SizeChanged);

                                #endregion

                                #region TabItem

                                ClsTabsTabItem ttinew = new ClsTabsTabItem();
                                ttinew.Header = ctc[j].TabTitle;
                                ttinew.Tag = cmi.strPageid + "," + ctc[j].TabId;
                                ttinew.TabID = cmi.strPageid + "," + ctc[j].TabId;
                                ttinew.TabTitle = ctc[j].TabTitle;
                                ttinew.TabTag = cmi.strPageid + "," + ctc[j].TabId;
                                //ttinew.lstBuddyDragged = cmi.lstUsersDropped;

                                ttinew.Background = Brushes.Transparent;
                                ttinew.Foreground = Brushes.Black;
                                ttinew.Height = 20;
                                ttinew.Width = 150;
                                ttinew.Content = cnvNew;

                                DataSet ds = ClsPod.Get_PodTab(ctc[j].TabId);

                                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                                {
                                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                                    {
                                        if (ds.Tables[0].Rows[i]["ID"].ToString() == cmi.strPodid)
                                        {
                                            ClsModuleLogic cml = new ClsModuleLogic().GetPodModule(int.Parse(ds.Tables[0].Rows[i]["ModuleID"].ToString()));

                                            //ClsPermissionCollection objCPC = ClsPermissionCollection.Get_ModPermission(int.Parse(ds.Tables[0].Rows[i]["ModuleID"].ToString()));

                                            //int[] arrPermissionValue = new int[objCPC.Count];

                                            //for (int percount = 0; percount < objCPC.Count; percount++)
                                            //{
                                            //    arrPermissionValue[percount] = objCPC[percount].PermissionValue;
                                            //}
                                            if (VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.BootStrap || VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.SuperNode || VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.NodeWithNetP2P)
                                            {
                                                c = new CtlPod(cmi.intModuleId, cmi.strModuleName, "True", cmi.strUri[0], cmi.ModPermissions, from);
                                            }
                                            else if (VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.NodeWithHttp)
                                            {
                                                c = new CtlPod(cmi.intModuleId, cmi.strModuleName, "True", cmi.strUri[1], cmi.ModPermissions, from);
                                            }
                                            c.Height = double.Parse(ds.Tables[0].Rows[i]["PodHeight"].ToString());
                                            c.Width = double.Parse(ds.Tables[0].Rows[i]["PodWidth"].ToString());
                                            c.ModPer = cmi.ModPermissions;
                                            c.DBHeight = c.Height;
                                            c.DBWidth = c.Width;
                                            c.Tag = "Participant";
                                            c.ModID = cml.ModuleId;
                                            c.Text = cml.ModuleTitle;
                                            c.PageId = cmi.strPageid;
                                            c.ResizePod = false;
                                            c.PID = int.Parse(ds.Tables[0].Rows[i]["ID"].ToString());
                                            c.TabId = cmi.strPageid + "," + ctc[j].TabId;
                                            //c.lstUsersDropped = cmi.lstUsersDropped;

                                            //for (int bCnt = 0; bCnt < cmi.lstUsersDropped.Count; bCnt++)
                                            //{
                                            //    ComboItem cbi = new ComboItem();
                                            //    cbi.setName(cmi.lstUsersDropped[bCnt]);
                                            //    c.cmbPodBuddy.Items.Add(cbi);
                                            //}

                                            c.lstUsersDropped.Add(from);
                                            ComboItem cbi = new ComboItem();
                                            cbi.setName(from);
                                            c.cmbPodBuddy.Items.Add(cbi);//pod buddy list
                                            for (int dCnt = 0; dCnt < cmi.lstUsersDropped.Count; dCnt++)
                                            {
                                                if (cmi.lstUsersDropped[dCnt] != VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName)
                                                {
                                                    if (!c.lstUsersDropped.Contains(cmi.lstUsersDropped[dCnt]))
                                                    {
                                                        c.lstUsersDropped.Add(cmi.lstUsersDropped[dCnt]);
                                                        ComboItem cbi1 = new ComboItem();
                                                        cbi1.setName(cmi.lstUsersDropped[dCnt]);
                                                        c.cmbPodBuddy.Items.Add(cbi1);  // pod buddy list                                               
                                                        //tti.lstBuddyDragged.Add(lstModInfo[intModCnt].lstUsersDropped[dCnt]);//tab buddy list
                                                        //ttinew.lstBuddyDragged.Add(lstModInfo[intModCnt].lstUsersDropped[dCnt]);//page buddy list
                                                    }
                                                }
                                            }


                                            c.EntSavePage += new CtlPod.DelSavePage(dynCtlPod_EntSavePage);
                                            c.EntMainPodMouseDown += new CtlPod.DelMainPodMouseDown(dynCtlPod_EntMainPodMouseDown);
                                            c.EntRemoveBuddy += new CtlPod.DelRemoveBuddy(c_EntRemoveBuddy);
                                            //c.EntCnvPodBodyDrop += new CtlPod.DelCnvPodBodyDrop(c_EntCnvPodBodyDrop);
                                            //c.PreviewMouseUp += new MouseButtonEventHandler(c_PreviewMouseUp);
                                            //c.SizeChanged += new SizeChangedEventHandler(c_SizeChanged);
                                            Resizer r = new Resizer();
                                            r.SetValue(Canvas.LeftProperty, double.Parse(ds.Tables[0].Rows[i]["PodLeft"].ToString()));
                                            r.SetValue(Canvas.TopProperty, double.Parse(ds.Tables[0].Rows[i]["PodTop"].ToString()));
                                            c.DBTop = double.Parse(ds.Tables[0].Rows[i]["PodTop"].ToString());
                                            c.DBLeft = double.Parse(ds.Tables[0].Rows[i]["PodLeft"].ToString());
                                            r.Content = c;
                                            r.Background = Brushes.Transparent;
                                            r.SizeChanged += new SizeChangedEventHandler(r_SizeChanged);
                                            r.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(r_PreviewMouseLeftButtonDown);
                                            r.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(r_PreviewMouseLeftButtonUp);
                                            cnvNew.Children.Add(r);
                                        }
                                    }
                                }

                                ptcTabs.Items.Add(ttinew);


                                #endregion
                            }
                        }
                    }
                    tti.Content = ptcTabs;
                    ptc.Items.Add(tti);
                }
                #endregion

                ((ClsTabsTabItem)ptc.Items[ptc.Items.Count - 1]).Focus();

                AddNewPage();
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "OpenCollPage--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
            }
        }

        #endregion

        internal void OpenDragPage(List<clsModuleInfo> lstModInfo, string from)
        {
            ptc.Items.Remove(ptc.Items[ptc.Items.Count - 1]);

            string pageid = null;
            string droptype = null;
            string tabid = null;

            if (lstModInfo != null && lstModInfo.Count > 0)
            {
                pageid = lstModInfo[0].strPageid;
                tabid = lstModInfo[0].strTabid;
                droptype = lstModInfo[0].strDropType;
            }
            if (pageid != null && droptype == "Page Type")
            {
                #region Page Type

                ClsPageLogic cpl = new ClsPageLogic().Get_PageInfo(int.Parse(pageid));
                ClsTabsTabItem tti = new ClsTabsTabItem();
                tti.Header = cpl.PageTitle;
                tti.Foreground = Brushes.Black;
                tti.Tag = pageid;
                tti.TabTag = pageid;
                tti.TabID = pageid;
                tti.Background = Brushes.Transparent;
                tti.AllowDrop = true;
                tti.Drop += new DragEventHandler(tti_Drop);
                tti.lstBuddyDragged.Add(from);

                for (int msgcount = 0; msgcount < lstModInfo.Count; msgcount++)
                {
                    for (int listcount = 0; listcount < lstModInfo[msgcount].lstUsersDropped.Count; listcount++)
                    {
                        if (lstModInfo[msgcount].lstUsersDropped[listcount] != VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName)
                        {
                            if (!tti.lstBuddyDragged.Contains(lstModInfo[msgcount].lstUsersDropped[listcount]))
                            {
                                tti.lstBuddyDragged.Add(lstModInfo[msgcount].lstUsersDropped[listcount]);//page buddy list
                            }
                        }
                    }
                }

                #region PageDeclarations

                ClsPageTabControl ptcTabs = new ClsPageTabControl();
                ptcTabs.Tag = pageid;
                ptcTabs.PageID = int.Parse(pageid);
                ptcTabs.PageTag = pageid;
                ptcTabs.PageTitle = cpl.PageTitle;
                ptcTabs.Background = Brushes.Transparent;
                ptcTabs.Foreground = Brushes.Black;
                ptcTabs.BorderBrush = Brushes.Transparent;
                ptcTabs.BorderThickness = new Thickness(0, 0, 0, 0);


                #endregion

                List<string> lstTabIds = new List<string>();
                for (int lstCnt = 0; lstCnt < lstModInfo.Count; lstCnt++)
                {
                    if (!lstTabIds.Contains(lstModInfo[lstCnt].strTabid))
                    {
                        lstTabIds.Add(lstModInfo[lstCnt].strTabid);
                    }
                }
                //string[] strTab = cmi.strTabid.Split(',');

                List<string> lstTabUniIds = new List<string>();
                for (int intTabSplit = 0; intTabSplit < lstTabIds.Count; intTabSplit++)
                {
                    lstTabUniIds.Add(lstTabIds[intTabSplit].Split(',')[1]);
                }

                ClsTabCollection ctc = ClsTabCollection.GetAll(int.Parse(pageid));

                for (int TabCnt = 0; TabCnt < lstTabUniIds.Count; TabCnt++)
                {
                    for (int j = 0; j < ctc.Count; j++)
                    {
                        if (ctc[j].TabId.ToString() == lstTabUniIds[TabCnt])
                        {
                            #region Canvas as container for Pods

                            Canvas cnvNew = new Canvas();
                            cnvNew.Background = Brushes.Transparent;
                            cnvNew.Height = ptcTabs.Height;
                            cnvNew.Width = ptcTabs.Width;
                            cnvNew.HorizontalAlignment = HorizontalAlignment.Stretch;
                            cnvNew.VerticalAlignment = VerticalAlignment.Stretch;
                            cnvNew.AllowDrop = true;
                            cnvNew.Drop += new DragEventHandler(cnvNew_Drop);
                            cnvNew.MouseMove += new MouseEventHandler(cnvNew_MouseMove);
                            cnvNew.SizeChanged += new SizeChangedEventHandler(cnvNew_SizeChanged);

                            #endregion

                            #region TabItem

                            ClsTabsTabItem ttinew = new ClsTabsTabItem();
                            ttinew.Header = ctc[j].TabTitle;
                            ttinew.Tag = pageid + "," + ctc[j].TabId;
                            ttinew.TabID = pageid + "," + ctc[j].TabId;
                            ttinew.TabTitle = ctc[j].TabTitle;
                            ttinew.TabTag = pageid + "," + ctc[j].TabId;

                            ttinew.Background = Brushes.Transparent;
                            ttinew.Foreground = Brushes.Black;
                            ttinew.Height = 20;
                            ttinew.Width = 150;
                            ttinew.AllowDrop = true;
                            ttinew.Drop += new DragEventHandler(ttinew_Drop);
                            ttinew.Content = cnvNew;
                            ttinew.lstBuddyDragged.Add(from);

                            for (int msgcount = 0; msgcount < lstModInfo.Count; msgcount++)
                            {
                                for (int listcount = 0; listcount < lstModInfo[msgcount].lstUsersDropped.Count; listcount++)
                                {
                                    if (lstModInfo[msgcount].lstUsersDropped[listcount] != VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName)
                                    {
                                        if (!ttinew.lstBuddyDragged.Contains(lstModInfo[msgcount].lstUsersDropped[listcount]))
                                        {                                            
                                            ttinew.lstBuddyDragged.Add(lstModInfo[msgcount].lstUsersDropped[listcount]);//tab buddy list
                                        }
                                    }
                                }
                            }

                            DataSet ds = ClsPod.Get_PodTab(ctc[j].TabId);

                            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                            {
                                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                                {
                                    //if (ds.Tables[0].Rows[i]["ID"].ToString() == cmi.strPodid)
                                    //{

                                    for (int intModCnt = 0; intModCnt < lstModInfo.Count; intModCnt++)
                                    {
                                        if (lstModInfo[intModCnt].strPodid == ds.Tables[0].Rows[i]["ID"].ToString())
                                        {
                                            ClsModuleLogic cml = new ClsModuleLogic().GetPodModule(int.Parse(ds.Tables[0].Rows[i]["ModuleID"].ToString()));

                                            if (VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.BootStrap || VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.SuperNode || VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.NodeWithNetP2P)
                                            {
                                                c = new CtlPod(lstModInfo[intModCnt].intModuleId, lstModInfo[intModCnt].strModuleName, "True", lstModInfo[intModCnt].strUri[0], lstModInfo[intModCnt].ModPermissions, from);
                                            }
                                            else if (VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.NodeWithHttp)
                                            {
                                                c = new CtlPod(lstModInfo[intModCnt].intModuleId, lstModInfo[intModCnt].strModuleName, "True", lstModInfo[intModCnt].strUri[1], lstModInfo[intModCnt].ModPermissions, from);
                                            }

                                            c.Height = double.Parse(ds.Tables[0].Rows[i]["PodHeight"].ToString());
                                            c.Width = double.Parse(ds.Tables[0].Rows[i]["PodWidth"].ToString());
                                            c.ModPer = lstModInfo[intModCnt].ModPermissions;
                                            c.DBHeight = c.Height;
                                            c.DBWidth = c.Width;
                                            c.Tag = "Participant";
                                            c.ModID = cml.ModuleId;
                                            c.Text = cml.ModuleTitle;
                                            c.PageId = pageid;
                                            c.ResizePod = false;
                                            c.PID = int.Parse(ds.Tables[0].Rows[i]["ID"].ToString());
                                            c.TabId = pageid + "," + ctc[j].TabId;
                                            //c.lstUsersDropped = lstModInfo[intModCnt].lstUsersDropped;

                                            c.lstUsersDropped.Add(from);
                                            ComboItem cbi = new ComboItem();
                                            cbi.setName(from);
                                            c.cmbPodBuddy.Items.Add(cbi);//pod buddy list                                   

                                            //tti.lstBuddyDragged.Add(from);// tab buddylist
                                            //ttinew.lstBuddyDragged.Add(from);//page buddy list



                                            for (int dCnt = 0; dCnt < lstModInfo[intModCnt].lstUsersDropped.Count; dCnt++)
                                            {
                                                if (lstModInfo[intModCnt].lstUsersDropped[dCnt] != VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName)
                                                {
                                                    if (!c.lstUsersDropped.Contains(lstModInfo[intModCnt].lstUsersDropped[dCnt]))
                                                    {
                                                        c.lstUsersDropped.Add(lstModInfo[intModCnt].lstUsersDropped[dCnt]);
                                                        ComboItem cbi1 = new ComboItem();
                                                        cbi1.setName(lstModInfo[intModCnt].lstUsersDropped[dCnt]);
                                                        c.cmbPodBuddy.Items.Add(cbi1);  // pod buddy list                                               
                                                        //tti.lstBuddyDragged.Add(lstModInfo[intModCnt].lstUsersDropped[dCnt]);//tab buddy list
                                                        //ttinew.lstBuddyDragged.Add(lstModInfo[intModCnt].lstUsersDropped[dCnt]);//page buddy list
                                                    }
                                                }
                                            }


                                            c.EntSavePage += new CtlPod.DelSavePage(dynCtlPod_EntSavePage);
                                            c.EntMainPodMouseDown += new CtlPod.DelMainPodMouseDown(dynCtlPod_EntMainPodMouseDown);
                                            Resizer r = new Resizer();
                                            r.SetValue(Canvas.LeftProperty, double.Parse(ds.Tables[0].Rows[i]["PodLeft"].ToString()));
                                            r.SetValue(Canvas.TopProperty, double.Parse(ds.Tables[0].Rows[i]["PodTop"].ToString()));
                                            c.DBTop = double.Parse(ds.Tables[0].Rows[i]["PodTop"].ToString());
                                            c.DBLeft = double.Parse(ds.Tables[0].Rows[i]["PodLeft"].ToString());
                                            r.Content = c;
                                            r.Background = Brushes.Transparent;
                                            r.SizeChanged += new SizeChangedEventHandler(r_SizeChanged);
                                            r.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(r_PreviewMouseLeftButtonDown);
                                            r.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(r_PreviewMouseLeftButtonUp);
                                            cnvNew.Children.Add(r);
                                            //}
                                        }
                                    }
                                }
                            }

                            ptcTabs.Items.Add(ttinew);


                            #endregion

                            break;
                        }
                    }
                }

                #endregion
                tti.Content = ptcTabs;
                ptc.Items.Add(tti);
            }

            if (tabid != null && droptype == "Tab Type")
            {
                #region Tab Type

                ClsPageLogic cpl = new ClsPageLogic().Get_PageInfo(int.Parse(pageid));
                ClsTabsTabItem tti = new ClsTabsTabItem();
                tti.Header = cpl.PageTitle;
                tti.Foreground = Brushes.Black;
                tti.Tag = pageid;
                tti.TabTag = pageid;
                tti.TabID = pageid;
                tti.Background = Brushes.Transparent;
                tti.AllowDrop = true;
                tti.Drop += new DragEventHandler(tti_Drop);
                //tti.lstBuddyDragged.Add(from);

                

                #region PageDeclarations

                ClsPageTabControl ptcTabs = new ClsPageTabControl();
                ptcTabs.Tag = pageid;
                ptcTabs.PageID = int.Parse(pageid);
                ptcTabs.PageTag = pageid;
                ptcTabs.PageTitle = cpl.PageTitle;
                ptcTabs.Background = Brushes.Transparent;
                ptcTabs.Foreground = Brushes.Black;
                ptcTabs.BorderBrush = Brushes.Transparent;
                ptcTabs.BorderThickness = new Thickness(0, 0, 0, 0);

                #endregion

                #region TabItem

                //ClsTabCollection ctc = ClsTabCollection.GetAll(int.Parse(pageid));

                ClsTabLogic ctl = new ClsTabLogic();
                tabid = tabid.Split(',')[1];
                DataSet dsTabInfo = ctl.Get_Tab(int.Parse(tabid));

                if (dsTabInfo != null && dsTabInfo.Tables.Count > 0 && dsTabInfo.Tables[0].Rows.Count > 0)
                {
                    #region Canvas as container for Pods

                    Canvas cnvNew = new Canvas();
                    cnvNew.Background = Brushes.Transparent;
                    cnvNew.Height = ptcTabs.Height;
                    cnvNew.Width = ptcTabs.Width;
                    cnvNew.HorizontalAlignment = HorizontalAlignment.Stretch;
                    cnvNew.VerticalAlignment = VerticalAlignment.Stretch;
                    cnvNew.AllowDrop = true;
                    cnvNew.Drop += new DragEventHandler(cnvNew_Drop);
                    cnvNew.MouseMove += new MouseEventHandler(cnvNew_MouseMove);
                    cnvNew.SizeChanged += new SizeChangedEventHandler(cnvNew_SizeChanged);

                    #endregion

                    ClsTabsTabItem ttinew = new ClsTabsTabItem();
                    ttinew.Header = dsTabInfo.Tables[0].Rows[0]["TabTitle"].ToString();
                    ttinew.Tag = pageid + "," + tabid;
                    ttinew.TabID = pageid + "," + tabid;
                    ttinew.TabTitle = dsTabInfo.Tables[0].Rows[0]["TabTitle"].ToString();
                    ttinew.TabTag = pageid + "," + tabid;
                    ttinew.lstBuddyDragged.Add(from);

                    ttinew.Background = Brushes.Transparent;
                    ttinew.Foreground = Brushes.Black;
                    ttinew.Height = 20;
                    ttinew.Width = 150;
                    ttinew.AllowDrop = true;
                    ttinew.Drop += new DragEventHandler(ttinew_Drop);
                    ttinew.Content = cnvNew;

                    for (int msgcount = 0; msgcount < lstModInfo.Count; msgcount++)
                    {
                        for (int listcount = 0; listcount < lstModInfo[msgcount].lstUsersDropped.Count; listcount++)
                        {
                            if (lstModInfo[msgcount].lstUsersDropped[listcount] != VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName)
                            {
                                if (!ttinew.lstBuddyDragged.Contains(lstModInfo[msgcount].lstUsersDropped[listcount]))
                                {
                                    //tti.lstBuddyDragged.Add(lstModInfo[msgcount].lstUsersDropped[listcount]);//page buddy list
                                    ttinew.lstBuddyDragged.Add(lstModInfo[msgcount].lstUsersDropped[listcount]);//tab buddy list
                                }
                            }
                        }
                    }

                    DataSet ds = ClsPod.Get_PodTab(int.Parse(tabid));

                    if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            for (int intModCnt = 0; intModCnt < lstModInfo.Count; intModCnt++)
                            {
                                if (lstModInfo[intModCnt].strPodid == ds.Tables[0].Rows[i]["ID"].ToString())
                                {
                                    ClsModuleLogic cml = new ClsModuleLogic().GetPodModule(int.Parse(ds.Tables[0].Rows[i]["ModuleID"].ToString()));

                                    if (VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.BootStrap || VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.SuperNode || VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.NodeWithNetP2P)
                                    {
                                        c = new CtlPod(lstModInfo[intModCnt].intModuleId, lstModInfo[intModCnt].strModuleName, "True", lstModInfo[intModCnt].strUri[0], lstModInfo[intModCnt].ModPermissions, from);
                                    }
                                    else if (VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.NodeWithHttp)
                                    {
                                        c = new CtlPod(lstModInfo[intModCnt].intModuleId, lstModInfo[intModCnt].strModuleName, "True", lstModInfo[intModCnt].strUri[1], lstModInfo[intModCnt].ModPermissions, from);
                                    }

                                    c.Height = double.Parse(ds.Tables[0].Rows[i]["PodHeight"].ToString());
                                    c.Width = double.Parse(ds.Tables[0].Rows[i]["PodWidth"].ToString());
                                    c.ModPer = lstModInfo[intModCnt].ModPermissions;
                                    c.DBHeight = c.Height;
                                    c.DBWidth = c.Width;
                                    c.Tag = "Participant";
                                    c.ModID = cml.ModuleId;
                                    c.Text = cml.ModuleTitle;
                                    c.PageId = pageid;
                                    c.ResizePod = false;
                                    c.PID = int.Parse(ds.Tables[0].Rows[i]["ID"].ToString());
                                    c.TabId = pageid + "," + tabid;
                                    //c.lstUsersDropped = lstModInfo[intModCnt].lstUsersDropped;


                                    c.lstUsersDropped.Add(from);
                                    ComboItem cbi = new ComboItem();
                                    cbi.setName(from);
                                    c.cmbPodBuddy.Items.Add(cbi);//pod buddy list                                   

                                    //tti.lstBuddyDragged.Add(from);// tab buddylist
                                    //ttinew.lstBuddyDragged.Add(from);//page buddy list



                                    for (int dCnt = 0; dCnt < lstModInfo[intModCnt].lstUsersDropped.Count; dCnt++)
                                    {
                                        if (lstModInfo[intModCnt].lstUsersDropped[dCnt] != VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName)
                                        {
                                            if (!c.lstUsersDropped.Contains(lstModInfo[intModCnt].lstUsersDropped[dCnt]))
                                            {
                                                c.lstUsersDropped.Add(lstModInfo[intModCnt].lstUsersDropped[dCnt]);
                                                ComboItem cbi1 = new ComboItem();
                                                cbi1.setName(lstModInfo[intModCnt].lstUsersDropped[dCnt]);
                                                c.cmbPodBuddy.Items.Add(cbi1);  // pod buddy list                                               
                                                //tti.lstBuddyDragged.Add(lstModInfo[intModCnt].lstUsersDropped[dCnt]);//tab buddy list
                                                //ttinew.lstBuddyDragged.Add(lstModInfo[intModCnt].lstUsersDropped[dCnt]);//page buddy list
                                            }
                                        }
                                    }

                                    c.EntSavePage += new CtlPod.DelSavePage(dynCtlPod_EntSavePage);
                                    c.EntMainPodMouseDown += new CtlPod.DelMainPodMouseDown(dynCtlPod_EntMainPodMouseDown);
                                    Resizer r = new Resizer();
                                    r.SetValue(Canvas.LeftProperty, double.Parse(ds.Tables[0].Rows[i]["PodLeft"].ToString()));
                                    r.SetValue(Canvas.TopProperty, double.Parse(ds.Tables[0].Rows[i]["PodTop"].ToString()));
                                    c.DBTop = double.Parse(ds.Tables[0].Rows[i]["PodTop"].ToString());
                                    c.DBLeft = double.Parse(ds.Tables[0].Rows[i]["PodLeft"].ToString());
                                    r.Content = c;
                                    r.Background = Brushes.Transparent;
                                    r.SizeChanged += new SizeChangedEventHandler(r_SizeChanged);
                                    r.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(r_PreviewMouseLeftButtonDown);
                                    r.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(r_PreviewMouseLeftButtonUp);
                                    cnvNew.Children.Add(r);
                                }
                            }

                        }
                    }
                    ptcTabs.Items.Add(ttinew);
                }

                #endregion


                #endregion
                tti.Content = ptcTabs;
                ptc.Items.Add(tti);

            }
            ((ClsTabsTabItem)ptc.Items[ptc.Items.Count - 1]).Focus();

            AddNewPage();
        }

        #region IDisposable Members

        public void Dispose()
        {
            if (ptc != null)
            {
                ptc = null;
            }
            //if (lstUsersDropped != null)
            //{
            //    lstUsersDropped = null;
            //}
            if (c != null)
            {
                c = null;
            }
            if (dynCtlPod != null)
            {
                dynCtlPod = null;
            }
            if (EntChannelSetMsg != null)
            {
                EntChannelSetMsg = null;
            }

            if (EntChannelSetPageMsg != null)
            {
                EntChannelSetPageMsg = null;

            }
        }

        #endregion
    }
}

